Member inner class

A class that is declared inside a class but outside a method is known as member inner class.
Invocation of Member Inner class

  1. From within the class
  2. From outside the class

Example of member inner class that is invoked inside a class
In this example, we are invoking the method of member inner class from the display method of Outer class.

class Outer{
   private int value = 70;
   class Inner{
      void show(){
          System.out.println("value is "+value );
      }
    }
 
   void display(){
     Inner in = new Inner();
     in.show();
   }
  public static void main(String args[]){
    Outer obj = new Outer();
    obj.display();
  }
}

output:

Member inner class

See in the directory there are the two following class files after compiling.
Outer.class
Outer$Inner.class

Internal code generated by the compiler for member inner class:
The java compiler creates a class file named Outer$Inner in this case. The Member inner class have the reference of Outer class that is why it can access all the data members of Outer class including private.

import java.io.PrintStream;

class Outer$Inner
{
    final Outer this$0;
    Outer$Inner()
    {   super();
        this$0 = Outer.this;
    }

    void show()
    {
        System.out.println((new StringBuilder()).append("value is ")
                    .append(Outer.access$000(Outer.this)).toString());
    }
 }

Example of member inner class that is invoked outside a class

In this example, we are invoking the show() method of Inner class from outside the outer class i.e. Test class.

//Program of member inner class that is invoked outside a class

class Outer{
  private int value = 70;
  class Inner{
   void show(){System.out.println("value is "+value);}
  }
}

class Test{
  public static void main(String args[]){
    Outer obj = new Outer();
    Outer.Inner in = obj.new Inner();
    in.show();
  }
}

output:

Member inner class in java

Rules for Inner Classes:
Following properties can be noted about Inner classes:

  • The outer class (the class containing the inner class) can instantiate as many number of inner class objects as it wishes, inside it’s code.
  • If the inner class is public & the containing class as well, then code in some other unrelated class can as well create an instance of the inner class.
  • In above case the inner class can be created as follows:
<OuterClassName> outerObj = new <OuterClassName>(arguments);
outerObj.<InnerClassName> innerObj = outerObj.new <InnerClassName>(arguments);
  • No inner class objects are automatically instantiated with an outer class object.
  • If the inner class is static, then static inner class can be instantiated without an outer class instance, otherwise, the inner class object must be associated with an instance of the outer class.
  • Inner class code has free access to all elements of the outer class object that contains it, by name (no matter what the access level of the elements is), if the inner class has a varible with same name then the outer class’s variable can be access like this:
<OuterClassName>.this.<variableName>
  • The outer class can call even the private methods of the inner class

<<Previous <<   || Index ||   >>Next >>
Previous
Next

2 Comments

  1. Anonymous November 20, 2013
  2. Dinesh November 24, 2013