You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2015-08-20-nested-classes.md
+6-5Lines changed: 6 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,9 +80,9 @@ class MyOuter {
80
80
{% endhighlight %}
81
81
82
82
The output of the above program would be `Outer x is 7`. This happens because an __inner class can access private members
83
-
of its outer class__. The inner class is also a member of the outer class. So just as any member of the outer class (say,
84
-
an instance method) can access any other member of the outer class, private or not, the inner class (also a member) can do
85
-
the same.
83
+
of its outer class even those which are declared as `static`__. The inner class is also a member of the outer class. So
84
+
just as any member of the outer class (say, an instance method) can access any other member of the outer class, private
85
+
or not, the inner class (also a member) can do the same.
86
86
87
87
### Instantiate the inner class
88
88
@@ -110,7 +110,7 @@ class MyOuter {
110
110
{% endhighlight %}
111
111
112
112
The reason the above syntax works is because the outer class instance method code is doing the instantiating. In other
113
-
words, there's already an instance of the outer class—the instance running the makeInner() method.
113
+
words, there's already an instance of the outer class i.e, the instance running the makeInner() method.
114
114
115
115
#### Instantiating an Inner Class from Outside the Outer Class Instance Code
116
116
@@ -149,7 +149,7 @@ public void myMethod() {
149
149
150
150
So within an inner class code, the `this` reference refers to the instance of the inner class, as you'd probably expect,
151
151
since `this` always refers to the currently executing object. But when the inner class code wants an explicit reference
152
-
to the outer class instance that the inner instance is tied to, it can access the outer class `this` like:
152
+
to the outer class instance that the inner instance is tied to, it can access the outer class `this` like shown below:
153
153
154
154
{% highlight java linenos %}
155
155
class MyOuter {
@@ -183,6 +183,7 @@ class MyOuter {
183
183
184
184
* You can refer to this [StackOverflow question](http://stackoverflow.com/questions/12251922/i-thought-inner-classes-could-access-the-outer-class-variables-methods)
185
185
to understand how you can and can't access outer class members from inner class.
186
+
* Inner class can access __private members__ of the outer enclosing class even those __which are declared `static`__.
186
187
* Normally the inner class code doesn't need a reference to the outer class, since it already has an implicit one
187
188
it's using to access the members of the outer class, it would need a reference to the outer class if it needed to pass
188
189
that reference to some other code as in the above example.
0 commit comments