| Java FAQ | ||
| JSP FAQ | ||
| Servlet FAQ | ||
XyzWs Java FAQ:
Why can't we have a static member within an inner class?
Printer-friendly version |
Mail this to a friend
|
Advertisement
|
Why can't we have a static member within an inner class?JLS: 8.1.3 Inner Classes and Enclosing Instances:
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 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(); |