Java- Nested Classes

8.Nested Classes

Type Description
Inner Class A class created within class and outside method.
Static Nested Class A static class created within class and outside method.
Local Inner Class A class created within method.
Anonymous Inner Class A class created for implementing interface or extending class. Its name is decided by the java compiler.
Nested Interface An interface created within class or interface.


1. Member Inner Classes

If a non-static class is created in the class & outside the method is known as -Member Inner class”. Because it is just a member of that class

public class Outer{    
    int a =100;
    String msg="Iam Outer Class";    
    class Inner{
        int b=200;
        String inmsg="Inner class variable";
        public void show(){
            System.out.println(b+"\n"+inmsg+"\n"+msg);
        }
    }
  public static void main(String []args){
        Outer o = new Outer();
       Outer.Inner i = o.new Inner();
       i.show();
     }
}


Internal Working

1. instance of inner class is created inside the instance of outer class.

The java compiler creates two class files in case of inner class. The class file name of inner class is “Outer$Inner”.For Outer.java it will create 2 .class files

  • Outer$Inner.class
  • Outer.class

  • For creating normal class object we do
    OuterClass ob = new OuterClass();

  • For creating inner class object we have to add OuterClass class & Object as below
    OuterClass.InnerClass i = o.new InnerClass();
Outer$Inner.Java Generated code
----------------------------------------
import java.io.PrintStream;

class Outer.Inner {
 int b;
 String inmsg;

 Outer.Inner() {
  this.b = 200;
  this.inmsg = "Inner class variable";
 }

 public void show() {
  System.out.println("" + this.b + "\n" + this.inmsg + "\n" + Outer.this.msg);
 }
}


2. Static Nested Classes (Nested Classes)

If a Static class is created inside Outer class is know as Static Nested class

  • Non Static Data Members/Methods : it Cannot assess directly

  • Static Data Members : it Can access

public class StaticNestedDemo {
	int a = 100;
	static int b = 200;
	static class Inner {
 static void get() {
 	System.out.println("B " + b);
 // a -Cannot make a static reference to the non-static field a
 }
	}
	public static void main(String[] args) {
 StaticNestedDemo.Inner ob = new StaticNestedDemo.Inner();
 ob.get();
 // ditectly
 StaticNestedDemo.Inner.get();
	}
}
B 200
B 200


3. Local Inner Classes

If a class is created inside the method is known as -Local Inner Class”

  • Local class variable should not Private, Public and Protected

  • Local inner class cannot be invoked from outside of the method.

  • Local Inner class only access Final variables from outside class(until 1.7 , from 1.8 they can access non-final also)

public class Local {
 public void get() {
  System.out.println("Get Method");
  int a = 100;
  class Inner {
   public void show() {
    System.out.println(a);
   }
  }
  Inner ob = new Inner();
  ob.show();
 }

 public static void main(String ar[]) {
  Local ob = new Local();
  ob.get();
 }
}
-----------------------
Get Method                                                                  
100


4. Anonymous Inner Classes

If a class doesn’t have any Name, such type of classes are noted as Anonymous Inner classes.in real time two types of Anonymous inner classes we may implement

Class : If method of one class may return Instance we can directly implement and will get the Object

Interface: at same way a method of interface return object we directly implement to get the object

interface A {
	public void aShow();
}
abstract class B {
	abstract void bShow();
}
public class AnnonymousDemo {
	A a = new A() {
 @Override
 public void aShow() {
 	System.out.println("A show()");
 }
	};
	B b = new B() {
 @Override
 void bShow() {
 	System.out.println("B show()");
 }
	};
	public static void main(String[] args) {
 AnnonymousDemo demo = new AnnonymousDemo();
 demo.a.aShow();
 demo.b.bShow();
	}
}
-------------------------
A show()
B show()


Internal Working

1.If we use anonymous inner class in our main class, internally it creates the new inner class with name MainClass$X(x is a number) which is

  • Extends in case of Class
  • Implements in case of Interface

In above class the compile generates Anonymous inner class as below

Class A : Inner class
-------------------------------------------------
class AnnonymousDemo$1  implements A
{
  AnnonymousDemo$1(AnnonymousDemo paramAnnonymousDemo) {}  
  public void aShow()
  {
    System.out.println("A show()");
  }
}


Class B : Inner Class
------------------------------------------------
class AnnonymousDemo$2  extends B
{
  AnnonymousDemo$2(AnnonymousDemo paramAnnonymousDemo) {}  
  void bShow()
  {
    System.out.println("B show()");
  }
}


2.If we want to create the Object for inner class we must use outer class object. because inner classes are generated inside of outer class.

AnnonymousDemo demo = new AnnonymousDemo();
 demo.a.aShow();
 demo.b.bShow();