Using a static inner class

This question already has an answer here:

  • Java inner class and static nested class 23 answers

  • A static inner class is associated with the outer class (in this case LinkedStack ) and not to an instance of it. For non-static inner classes, there should be an enclosing instance of the outer class. If Node were not static, it means that for any instance of Node to exist, there must be an instance of LinkedStack that encloses that instance.

    Having the Node class as static makes it more of a top-level class that is not bound to an instance of the outer class. Hence other classes can create instances of Node without creating any instance of LinkedStack .

    See Nested classes from the Java tutorials for more details on the differences.


    No, Static inner classes are classes which dont require an instance of the enclosing type, it is not the same thing as Static when used in the context of a method or field. Static for an inner class essentailly makes them a top level class. When you declare a non static inner class it has implicit access to the instance fields and methods of the enclosing type, thus making an instance of said type a requirement. Static classes dont have that luxury, and therefore dont require an instance of the enclosing type.

    链接地址: http://www.djcxy.com/p/92012.html

    上一篇: Java:在嵌套静态类中引用外部类

    下一篇: 使用静态内部类