Skip to content

Commit 055d430

Browse files
author
Ram swaroop
committed
code snippet changes
1 parent 8539877 commit 055d430

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

_posts/2015-08-20-inner-classes.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,27 @@ So within an inner class code, the `this` reference refers to the instance of th
136136
since `this` always refers to the currently executing object. But when the inner class code wants an explicit reference
137137
to the outer class instance that the inner instance is tied to, it can access the outer class `this` like:
138138

139-
{% highlight java %}
140-
class MyInner {
141-
public void seeOuter() {
142-
System.out.println("Outer x is " + x);
143-
System.out.println("Inner class ref is " + this);
144-
System.out.println("Outer class ref is " + MyOuter.this);
145-
}
139+
{% highlight java linenos %}
140+
public class MyOuter {
141+
private int x = 7;
142+
143+
public void makeInner() {
144+
MyInner in = new MyInner();
145+
in.seeOuter();
146+
}
147+
148+
class MyInner {
149+
public void seeOuter() {
150+
System.out.println("Outer x is " + x);
151+
System.out.println("Inner class ref is " + this);
152+
System.out.println("Outer class ref is " + MyOuter.this);
153+
}
154+
}
155+
156+
public static void main(String[] args) {
157+
MyOuter.MyInner inner = new MyOuter().new MyInner();
158+
inner.seeOuter();
159+
}
146160
}
147161
{% endhighlight %}
148162

0 commit comments

Comments
 (0)