8

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

1 Answers1

16

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

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
gustafc
  • 28,465
  • 7
  • 73
  • 99
  • Not with a semi-colon in between. The { } are just a scope definition. – vfilby May 11 '10 at 16:41
  • 5
    Nope, not when it's floating around in the class body like that - it's an empty instance initializer. Inside methods, then yes, it's a scope definition. – gustafc May 11 '10 at 16:44
  • 2
    @vfilby: No, if it's a block within a class but not within a method or constructor, it's an instance initializer. – Jon Skeet May 11 '10 at 16:44
  • 1
    My bad, didn't look closely enough at the example. Thanks guys. – vfilby May 11 '10 at 16:47