|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Inner Classes |
| 4 | +--- |
| 5 | + |
| 6 | +A class inside another class is called an __Inner Class__. In other words, as a class has member variables and member |
| 7 | +methods it can also have member classes. You can say inner classes are of 4 types: |
| 8 | + |
| 9 | + * Inner classes |
| 10 | + * Method-local inner classes |
| 11 | + * Anonymous inner classes |
| 12 | + * Static nested classes |
| 13 | + |
| 14 | +### When it is good to have an inner class? |
| 15 | + |
| 16 | +Let's say you have to design a chat client in java through which a user can send messages to the server. |
| 17 | +chat-client–specific methods (accept input, read new messages from server, send user input back to server, and so on) to |
| 18 | +be in the class |
| 19 | + |
| 20 | + |
| 21 | +### Little Basics |
| 22 | + |
| 23 | +Suppose you have an inner class like this: |
| 24 | + |
| 25 | +{% highlight java linenos %} |
| 26 | +public class MyOuter { |
| 27 | + class MyInner { } |
| 28 | +} |
| 29 | +{% endhighlight %} |
| 30 | + |
| 31 | +When you compile it : |
| 32 | + |
| 33 | +{% highlight java %} |
| 34 | +`javac MyOuter.java` |
| 35 | +{% endhighlight %} |
| 36 | + |
| 37 | +you will end up with two class files: |
| 38 | + |
| 39 | +{% highlight java %} |
| 40 | +MyOuter.class |
| 41 | +MyOuter$MyInner.class |
| 42 | +{% endhighlight %} |
| 43 | + |
| 44 | +The inner class is still, in the end, a separate class, so a separate class file is generated for it. But the inner |
| 45 | +class file isn't accessible to you in the usual way. You can't say |
| 46 | + |
| 47 | +{% highlight java %} |
| 48 | +java MyOuter$MyInner |
| 49 | +{% endhighlight %} |
| 50 | + |
| 51 | +in hopes of running the `main()` method of the inner class, because a regular inner class cannot have static declarations |
| 52 | +of any kind. The only way you can access the inner class is through a live instance of the outer class. In other words, |
| 53 | +only at runtime, when there's already an instance of the outer class to tie the inner class instance to. |
| 54 | + |
| 55 | +Now with the basics done, let's see an inner class performing some actions: |
| 56 | + |
| 57 | +{% highlight java linenos %} |
| 58 | +public class MyOuter { |
| 59 | + private int x = 7; |
| 60 | + |
| 61 | + // inner class definition |
| 62 | + class MyInner { |
| 63 | + public void seeOuter() { |
| 64 | + System.out.println("Outer x is " + x); |
| 65 | + } |
| 66 | + } // close inner class definition |
| 67 | +} // close outer class |
| 68 | +{% endhighlight %} |
| 69 | + |
| 70 | +The output of the above program would be `Outer x is 7`. This happens because an __inner class can access private members |
| 71 | +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, |
| 72 | +an instance method) can access any other member of the outer class, private or not, the inner class (also a member) can do |
| 73 | +the same. |
| 74 | + |
| 75 | +### Instantiate the inner class |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
0 commit comments