Search code examples
javabraces

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


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
    }
}

Solution

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