Skip to content

Commit fd906cf

Browse files
author
Ram swaroop
committed
modified content
1 parent 7553f2e commit fd906cf

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ class MyOuter {
8080
{% endhighlight %}
8181

8282
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.
8686

8787
### Instantiate the inner class
8888

@@ -110,7 +110,7 @@ class MyOuter {
110110
{% endhighlight %}
111111

112112
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 classthe instance running the makeInner() method.
113+
words, there's already an instance of the outer class i.e, the instance running the makeInner() method.
114114

115115
#### Instantiating an Inner Class from Outside the Outer Class Instance Code
116116

@@ -149,7 +149,7 @@ public void myMethod() {
149149

150150
So within an inner class code, the `this` reference refers to the instance of the inner class, as you'd probably expect,
151151
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:
153153

154154
{% highlight java linenos %}
155155
class MyOuter {
@@ -183,6 +183,7 @@ class MyOuter {
183183

184184
* You can refer to this [StackOverflow question](http://stackoverflow.com/questions/12251922/i-thought-inner-classes-could-access-the-outer-class-variables-methods)
185185
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`__.
186187
* Normally the inner class code doesn't need a reference to the outer class, since it already has an implicit one
187188
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
188189
that reference to some other code as in the above example.

0 commit comments

Comments
 (0)