开发者

Is this valid Java code? My teacher claims it is, but I'm really not so sure [duplicate]

开发者 https://www.devze.com 2022-12-30 02:52 出处:网络
This question already has answers here: 开发者_开发问答 Why is this Java code in curly braces ({}) outside of a method?
This question already has answers here: 开发者_开发问答 Why is this Java code in curly braces ({}) outside of a method? (3 answers) Closed 9 years ago.

Granted he didn't show us actual code here, just mentioned it, I found it extremely bizarre.

For example, according to what he said this is valid Java:

public class Person
{
    String Name;
    int Age;

    {
        //These two braces just chilling together - VALID? :O
    }
}


Yes - it's the instance initializer. You can also use it along with anonymous subclasses for Double-Brace Initialization.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号