File tree Expand file tree Collapse file tree 1 file changed +22
-1
lines changed
Expand file tree Collapse file tree 1 file changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ public class MyOuter {
3131When you compile it :
3232
3333{% highlight java %}
34- ` javac MyOuter.java `
34+ javac MyOuter.java
3535{% endhighlight %}
3636
3737you will end up with two class files:
@@ -74,6 +74,27 @@ the same.
7474
7575### Instantiate the inner class
7676
77+ To create an instance of an inner class, you must have an instance of the outer class to tie to the inner class. There
78+ are no exceptions to this rule: __ An inner class instance can never stand alone without a direct relationship to an
79+ instance of the outer class__ .
80+
81+ {% highlight java linenos %}
82+ public class MyOuter {
83+ private int x = 7;
84+
85+ public void makeInner() {
86+ MyInner in = new MyInner(); // make an inner instance
87+ in.seeOuter();
88+ }
89+
90+ class MyInner {
91+ public void seeOuter() {
92+ System.out.println("Outer x is " + x);
93+ }
94+ }
95+ }
96+ {% endhighlight %}
97+
7798
7899
79100
You can’t perform that action at this time.
0 commit comments