Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From the <a href="http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html" rel="noreferrer">Java Tutorial</a>:</p> <blockquote> <p>Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes. </p> </blockquote> <p>Static nested classes are accessed using the enclosing class name:</p> <pre><code>OuterClass.StaticNestedClass </code></pre> <p>For example, to create an object for the static nested class, use this syntax:</p> <pre><code>OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); </code></pre> <p>Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:</p> <pre><code>class OuterClass { ... class InnerClass { ... } } </code></pre> <p>An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.</p> <p>To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:</p> <pre><code>OuterClass.InnerClass innerObject = outerObject.new InnerClass(); </code></pre> <p>see: <a href="http://download.oracle.com/javase/tutorial/java/javaOO/nested.html" rel="noreferrer">Java Tutorial - Nested Classes</a></p> <p>For completeness note that there is also such a thing as an <a href="https://stackoverflow.com/questions/20468856/is-it-true-that-every-inner-class-requires-an-enclosing-instance">inner class <em>without</em> an enclosing instance</a>:</p> <pre><code>class A { int t() { return 1; } static A a = new A() { int t() { return 2; } }; } </code></pre> <p>Here, <code>new A() { ... }</code> is an <em>inner class defined in a static context</em> and does not have an enclosing instance.</p>
 

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