0

From Java documentation:

Local classes are classes that are defined in a block, which is a group of zero or more statements between balanced braces. You typically find local classes defined in the body of a method.

Also, Local class is a special kind of inner class, which needs to be instantiated from an outer class object:

As with instance methods and variables, an inner class is associated with an instance of its enclosing class

But static block is invoked during class load, before an object instance creation.

So, how is it possible create and instantiate a local class inside a static initializer block?

For instance:

class OuterClass {
    static{
        class InitLocalClass {
            ...
        }
        InitLocalClass lc = new InitLocalClass();
    }
}

Answer adapted from dup for Local Classes: as per JLS item # 15.9.2:

If C is a local class (§14.3), then let O be the innermost lexically enclosing class of C. Let n be an integer such that O is the n'th lexically enclosing class of the class in which the class instance creation expression appears. Then:

If C occurs in a static context, then i has no immediately enclosing instance.

Otherwise, if the class instance creation expression occurs in a static context, then a compile-time error occurs.

Otherwise, the immediately enclosing instance of i is the n'th lexically enclosing instance of this (§8.1.3).

Taisa
  • 26
  • 5
  • 1
    Why you don't try and see if there is a compiler error or it works? This is a very simple and complete example, so is easy to check. For this kind of questions the best is to try. If it works as you expect fine, otherwise try to investigate. – Davide Lorenzo MARINO May 19 '16 at 15:06
  • @Davide, I don't have a development environment setup here, but I found an online tool. It works, but I would like to understand why. Is it associated to the class object itself? I'll edit my question. Thx – Taisa May 19 '16 at 15:15
  • "Is it associated to the class object itself" No, in the same way that static nested classes aren't. – Andy Turner May 19 '16 at 15:19
  • @Andy But static nested class is not Inner Class. Inner Classes are exactly non-static nested classes. And local class is a type of inner class. – Taisa May 19 '16 at 15:21
  • 1
    See [here](http://stackoverflow.com/questions/758570/is-it-possible-to-make-anonymous-inner-classes-in-java-static). It discusses anonymous classes, which are considered inner classes, in static contexts. The same applies to local classes. – Sotirios Delimanolis May 19 '16 at 15:36
  • @Sotirios, thanks for the link reference! That discussion applies for Local Class as well, as both Anonymous and Local are special types of Inner Classes. That's a complete discussion. And the conclusion is : "So an anonymous class in a static context is roughly equivalent to a static nested class in that it does not keep a reference to the enclosing class, even though it's technically not a static class." – Taisa May 19 '16 at 15:46
  • Thanks for the JLS link. – qwerty May 19 '16 at 16:39

2 Answers2

0

well definition says it all

Local classes are classes that are defined in a block, which is a group of zero or more statements between balanced braces. You typically find local classes defined in the body of a method.

In short, you can do it . Be it inside method or static

Update :-

Instance class is associated with instance. they can be instantiated outside the class also like

OuterClass outer=new OuterClass ();
OuterClass.InnerClassinner=outer.new InnerClass();

But local class can be called from with in the block

M Sach
  • 33,416
  • 76
  • 221
  • 314
  • @M Sach, my doubt is about the instance necessity. Documentation says "As with instance methods and variables, an inner class is associated with an instance of its enclosing class". And local class is a type of inner class. – Taisa May 19 '16 at 15:25
  • @Taisa see my update – M Sach May 19 '16 at 15:34
  • What is an _Instance class_? Do you mean an inner class? Local classes are inner classes and can't be instantiated that way. Why are you even bringing up inner classes in that context? – Sotirios Delimanolis May 19 '16 at 16:22
0

Whether it be a static initializer block or a static method (just to mention), you can create objects of a class and define Local Classes as well.

Also, Local class is a special type (other than what we know as anonymous class) of Inner Class whose objects in every case are associated with the objects of the outer class.

P.S. -

Instance of an inner class in a static block is not associated with any instance of the class enclosing it.

qwerty
  • 2,392
  • 3
  • 30
  • 55
  • So the local class is associated to the class' object, not an instance? I'm just trying to understand the documentation when it says "As with instance methods and variables, an inner class is associated with an instance of its enclosing class" – Taisa May 19 '16 at 15:29
  • @Taisa check the updated answer – qwerty May 19 '16 at 15:45