Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think the real difference became clear in the above answers. </p> <p>First to get the terms right: </p> <ul> <li>A nested class is a class which is contained in another class at the source code level.</li> <li>It is static if you declare it with the <strong>static</strong> modifier.</li> <li>A non-static nested class is called inner class. (I stay with non-static nested class.)</li> </ul> <p>Martin's answer is right so far. However, the actual question is: What is the purpose of declaring a nested class static or not?</p> <p>You use <strong>static nested classes</strong> if you just want to keep your classes together if they belong topically together or if the nested class is exclusively used in the enclosing class. There is no semantic difference between a static nested class and every other class.</p> <p><strong>Non-static nested classes</strong> are a different beast. Similar to anonymous inner classes, such nested classes are actually closures. That means they capture their surrounding scope and their enclosing instance and make that accessible. Perhaps an example will clarify that. See this stub of a Container:</p> <pre><code>public class Container { public class Item{ Object data; public Container getContainer(){ return Container.this; } public Item(Object data) { super(); this.data = data; } } public static Item create(Object data){ // does not compile since no instance of Container is available return new Item(data); } public Item createSubItem(Object data){ // compiles, since 'this' Container is available return new Item(data); } } </code></pre> <p>In this case you want to have a reference from a child item to the parent container. Using a non-static nested class, this works without some work. You can access the enclosing instance of Container with the syntax <code>Container.this</code>.</p> <p>More hardcore explanations following:</p> <p>If you look at the Java bytecodes the compiler generates for an (non-static) nested class it might become even clearer:</p> <pre><code>// class version 49.0 (49) // access flags 33 public class Container$Item { // compiled from: Container.java // access flags 1 public INNERCLASS Container$Item Container Item // access flags 0 Object data // access flags 4112 final Container this$0 // access flags 1 public getContainer() : Container L0 LINENUMBER 7 L0 ALOAD 0: this GETFIELD Container$Item.this$0 : Container ARETURN L1 LOCALVARIABLE this Container$Item L0 L1 0 MAXSTACK = 1 MAXLOCALS = 1 // access flags 1 public &lt;init&gt;(Container,Object) : void L0 LINENUMBER 12 L0 ALOAD 0: this ALOAD 1 PUTFIELD Container$Item.this$0 : Container L1 LINENUMBER 10 L1 ALOAD 0: this INVOKESPECIAL Object.&lt;init&gt;() : void L2 LINENUMBER 11 L2 ALOAD 0: this ALOAD 2: data PUTFIELD Container$Item.data : Object RETURN L3 LOCALVARIABLE this Container$Item L0 L3 0 LOCALVARIABLE data Object L0 L3 2 MAXSTACK = 2 MAXLOCALS = 3 } </code></pre> <p>As you can see the compiler creates a hidden field <code>Container this$0</code>. This is set in the constructor which has an additional parameter of type Container to specify the enclosing instance. You can't see this parameter in the source but the compiler implicitly generates it for a nested class. </p> <p>Martin's example</p> <pre><code>OuterClass.InnerClass innerObject = outerObject.new InnerClass(); </code></pre> <p>would so be compiled to a call of something like (in bytecodes)</p> <pre><code>new InnerClass(outerObject) </code></pre> <p>For the sake of completeness:</p> <p>An anonymous class <strong>is</strong> a perfect example of a non-static nested class which just has no name associated with it and can't be referenced later.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload