X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Inner classes :

Sometimes we can declare a class inside another class, such type of classes are called inner classes. Inner classes concept was introduced in Java 1.1 version to fix GUI bugs as the part of event handling. But because of features and benefits of inner classes, slowly programmers started using even in regular code. Without existing one type of object, if there is no chance of existing another type of object then we should use inner class concept. Example : without existing Car object, if there is no chance of existing Wheel object then we should use inner classes. Without existing Bank object, if there is no chance of existing Account object then we have to define Account class inside Bank class.

The relation between outer and inner class is not parent to child relationship. It is Has-a relationship.

Based on the purpose and position of declaration, all inner classes are divided into four types :

  • Normal or Regular Inner classes




  • Method Local Inner classes




  • Anonymous Inner classes (without class_name)




  • Static Nested classes




From Static Nested class we can access only static members of outer class directly. But in normal inner classes we can access both static and non-static members of outer class directly.


Normal or Regular Inner classes :

If we declare any named class directly inside a class without static modifier, such type of class is called Normal or Regular Inner class.

Example :

class Outer { class Inner { } public static void main(String [ ] args) { System.out.println("Outer class main() method"); } }

Output :

save as : Outer.java compile as : javac Outer.java run as : java Outer Outer class main() method

Inside inner classes we can't declare static members hence it is not possible to declare main() method and hence we can't invoke inner class directly from command prompt.

Example :

class Outer { class Inner { public static void main(String [ ] args) { System.out.println("Outer class main() method"); } } }

Output :

save as : Outer.java compile as : javac Outer.java Compile time error : Illegal static declaration in inner class Outer.Inner modifier 'static' is only allowed in constant variable declarations

Accessing Inner class code from static area of Outer class :


Example :

class Outer { class Inner { public void m1() { System.out.println("Inner class m1() method"); } } public static void main(String [ ] args) { Outer o = new Outer(); Outer.Inner i = o.new Inner(); // or Outer.Inner i = new Outer().new Inner(); i.m1(); // or new Outer().new Inner().m1(); } }

Output :

save as : Outer.java compile as : javac Outer.java run as : java Outer Inner class m1() method

Accessing Inner class code from instance area of Outer class :


Example :

class Outer { class Inner { public void m1() { System.out.println("Inner class m1() method"); } } public void m2() { Inner i = new Inner(); i.m1(); } public static void main(String [ ] args) { Outer o = new Outer(); o.m2(); } }

Output :

save as : Outer.java compile as : javac Outer.java run as : java Outer Inner class m1() method

Accessing Inner class code from outside of Outer class :


Example :

class Outer { class Inner { public void m1() { System.out.println("Inner class m1() method"); } } } class Example { public static void main(String [ ] args) { Outer o = new Outer(); Outer.Inner i = o.new Inner(); i.m1(); } }

Output :

save as : Example.java compile as : javac Example.java run as : java Example Inner class m1() method

From the Inner class we can access all members of the Outer class (both static and non-static) directly.

Example :

class Outer { static int a = 10; int b = 20; class Inner { public void m1() { System.out.println(a); System.out.println(b); } } public static void main(String [ ] args) { new Outer().new Inner().m1(); } }

Output :

save as : Outer.java compile as : javac Outer.java run as : java Outer 10 20

Within the Inner class 'this' always pointing to current Inner class object. To refer current Outer class object we have to use - 'Outer_class_name.this'

Example :

class Outer { int a = 10; class Inner { int a = 20; public void m1() { int a = 30; System.out.println(a); System.out.println(this.a); System.out.println(Outer.this.a); } } public static void main(String [ ] args) { new Outer().new Inner().m1(); } }

Output :

save as : Outer.java compile as : javac Outer.java run as : java Outer 30 20 10

For the Outer classes (Top-Level classes) the application modifiers are public, <default>, final, abstract, strictfp.

But for Inner classes, in addition to the above modifier the following modified are also applicable - private, protected, static.


Method Local Inner classes :

Sometimes we can declare a class inside a method, such type of classes are called Method Local Inner classes.

The main purpose of Method Local Inner class is to define method specific functionality. The scope of Method Local Inner class is the method in which we declared it i.e. from outside of that method we can't access Method Local Inner classes. As the scope is very less, this type of Inner classes are most rarely used Inner classes.

Example :

class Outer { public void m1() { class Inner { public void m2(int a , int b) { ++a; ++b; System.out.println(a + " " + b); } } Inner i = new Inner(); i.m2(10 , 20); i.m2(100 , 200); } public static void main(String [ ] args) { new Outer().m1(); } }

Output :

save as : Outer.java compile as : javac Outer.java run as : java Outer 11 21 101 201

We can declare Inner class either in instance method or in static method. If we declare Inner class inside instance method then we can access both static and non-static variables of Outer class directly from that Inner class. If we declare Inner class inside static method then we can access only static members of Outer class directly from that Inner class.

Example :

class Outer { static int a = 10; int b = 20; public void m1() { class Inner { public void m2() { System.out.println(a + " " + b); } } Inner i = new Inner(); i.m2(); } public static void main(String [ ] args) { new Outer().m1(); } }

Output :

save as : Outer.java compile as : javac Outer.java run as : java Outer 10 20

From Method Local Inner class, we can't access local variables of the method in which we declare it. But if the local variable declared as 'final' then we can access it.

Example :

class Outer { int a = 10; public void m1() { int b = 20; // need to be declared 'final' class Inner { public void m2() { System.out.println(a + " " + b); } } Inner i = new Inner(); i.m2(); } public static void main(String [ ] args) { new Outer().m1(); } }

Output :

save as : Outer.java compile as : javac Outer.java Compile time error : local variable b is accessed from within inner class; needs to be declared final

Example :

class Outer { int a = 10; public void m1() { final int b = 20; class Inner { public void m2() { System.out.println(a + " " + b); } } Inner i = new Inner(); i.m2(); } public static void main(String [ ] args) { new Outer().m1(); } }

Output :

save as : Outer.java compile as : javac Outer.java run as : java Outer 10 20

Anonymous Inner class :

Sometimes we can declare a class without name also, such type of nameless Inner classes are called Anonymous Inner classes.

This type of Inner classes are most commonly used type of Inner classes. There are three types of Anonymous Inner classes :

  • Anonymous Inner class that extends a class




  • Anonymous Inner class that implements an interface




  • Anonymous Inner class that defined inside method arguments




Anonymous Inner class that extends a class :


Example :

class Algo { public void m1() { System.out.println("Algo class m1() method"); } } class Example { public static void main(String [ ] args) { Algo a = new Algo() { public void m1() { System.out.println("Example class m1() method"); } }; a.m1(); Algo a1 = new Algo(); a1.m1(); } }

Output :

save as : Example.java compile as : javac Example.java run as : java Example Example class m1() method Algo class m1() method
Note :

Parent class reference can be used to hold child class object but by using that reference we can call only methods available in the parent class and we can't call child specific methods. In the Anonymous Inner classes also we can declare new methods but we can't call these methods from outside of the class because we are depending on parent reference. This method is just for internal purpose only.

Analysis :
Algo a = new Algo(); // Just we are creating an object of Algo class Algo a = new Algo() { }; // We are creating child class for the Algo and for that child class we are creating an object with parent reference

Anonymous Inner class that implements an interface :


Example :

class Example { public static void main(String [ ] args) { Runnable r = new Runnable() { public void run() { for(int i = 0 ; i < 10 ; i++) { System.out.println("Child Thread"); } } }; Thread t = new Thread(r); t.start(); for(int i = 0 ; i < 10 ; i++) { System.out.println("Main Thread"); } } }

Output :

save as : Example.java compile as : javac Example.java run as : java Example Unpredictable outcome

A general class can extend only one class at a time. Anonymous Inner class also can extend only one class at a time. A general class can implement any number of interface whereas Anonymous Inner class can implement only one interface at a time. A general class can extend another class and can implement an interface simultaneously whereas Anonymous Inner class can extend another or can implement an interface but not both simultaneously.


Static Nested classes :

Sometime we declare Inner class with static modifier, such type of Inner classes are called Static Nested classes.

In the normal Inner class, Inner class object always associated with Outer class object i.e. without existing Outer class object, there is no chance of existing Inner class object. But Static Nested class is not associated with Outer class object i.e. without existing Outer class object there may be a chance of existing Static Nested class object.

Example :

class Outer { static class Nested { public void m1() { System.out.println("Static Nested class m1() method"); } } public static void main(String [ ] args) { Outer.Nested n = new Outer.Nested(); n.m1(); } }

Output :

save as : Outer.java compile as : javac Outer.java run as : java Outer Static Nested class m1() method

Within the Static Nested class we can declare static members including main() method. Hence, it is possible to invoke Nested class directly from command prompt.

Example :

class Outer { static class Nested { public static void main(String [ ] args) { System.out.println("Static Nested class main() method"); } } public static void main(String [ ] args) { System.out.println("Outer class main() method"); } }

Output :

save as : Outer.java compile as : javac Outer.java run as : java Outer Outer class main() method

From the normal Inner class both static and non-static members can be access directly but from Static Nested class we can access only static members of the Outer class directly.

Example :

class Outer { static int a = 10; int b = 20; static class Nested { public void m1() { System.out.println(a + " " + b); } } }

Output :

save as : Outer.java compile as : javac Outer.java Compile time error : non-static variable b cannot be referenced from a static context


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.