Understanding local classes in Java
As know, local classes are inner classes. JLS 14.3
All local classes are inner classes (§8.1.3).
What an inner class is is
JLS 8.1.3
An inner class C is a direct inner class of a class or interface O if O is the immediately enclosing type declaration of C and the declaration of C does not occur in a static context.
A class C is an inner class of class or interface O if it is either a direct inner class of O or an inner class of an inner class of O.
Which means the declaration of an inner class shouldn't be occured in a non-static context. But consider the following program:
public static void main (String[] args) throws java.lang.Exception
{
class Foo{ } //occured in the static-context
}
DEMO
Despite being declared in the static context, the declartion of the Foo
class is fine. Although it's impossible for an inner class to be declared in a static-context.
Classes defined in a block called local classes, they can appear anywhere in the method's block. Read more on the official docs - Local Classes:
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.
There is a very similar example for your case.
You can also see the JLS - 14.3. Local Class Declarations :
A local class is a nested class (§8) that is not a member of any class and that has a name (§6.2, §6.7).
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.
See this oracle link
Local classes are similar to inner classes because they cannot define or declare any static members. Local classes in static methods, such as the class PhoneNumber, which is defined in the static method validatePhoneNumber, can only refer to static members of the enclosing class.
Here is the example :-
public class localInner1{
private int data=30;//instance variable
void display(){
class Local{
void msg(){System.out.println(data);}
}
Local l=new Local();
l.msg();
}
public static void main(String args[]){
localInner1 obj=new localInner1();
obj.display();
}
}
I think I mixed up two similar, but different concepts. They were:
-- An inner class
Defined by JLS 8.1.3.
An inner class is a nested class that is not explicitly or implicitly declared static.
Now we can see, that
An inner class may be a non-static member class (§8.5), a local class (§14.3), or an anonymous class (§15.9.5).
Taking into account that JLS 15.9.5 doesn't prevent to declare anonymous classes within a static-context, we can say that the class Foo
is an inner class
, but netiher direct inner class of a class Main
nor inner class of a class Main
-- Inner class of class or interface O
Defined by JLS 8.1.3.
An inner class C is a direct inner class of a class or interface O if O is the immediately enclosing type declaration of C and the declaration of C does not occur in a static context.
A class C is an inner class of class or interface O if it is either a direct inner class of O or an inner class of an inner class of O.
Any class that belongs to this kind of classes should be appeared in a non-static context. It could be an anonymous class as well as a local class and so forth which was not appeared in a staic context.
链接地址: http://www.djcxy.com/p/91988.html上一篇: 来自封闭类的内部类成员的可访问性
下一篇: 了解Java中的本地类