--
JLS: 8.1.3 Inner Classes and Enclosing Instances: An inner class is a nested class that is not explicitly or implicitly declared static
. Inner classes may not declare static initializers (8.7) or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields (15.28).
Java programming language allows you to use nested classes: a class inside another class. For example:
class OuterClass { ... class InnerClass { ... } }
When a nested class is a static member of its enclosing class, the nested class is called a static nested class. When a nested class is a non-static, or instance member of its enclosing class, the nested class is called an inner class.
An inner class can access the non-static fields/methods of the enclosing class. The instances of an inner class only exist within the instance of the enclosing class. The only static context can be defined in an inner class are these compile-time constants declared with
static final
.To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the
outer object with this syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
Comments
Post a Comment