Member Inner Class in Java | Example Program

A non-static class that is declared inside a class but outside the method is known as member inner class in Java.

It is also known as regular inner class. It can be declared with access modifiers like public, default, private, and protected.

The syntax to declare member inner class in Java is like this:

Syntax:
 class Outer
 {
// A member inner class named Inner.
  class Inner  
  {
    . . . . . . . .
   }
}

When we compile the class like this javac Outer.java, the compiler will generate two separate dot class files like this Outer.class for outer class and Outer$Inner.class for the inner class.

This is because inner is not direct class. It is present somewhere inside outer class. Therefore, the compiler will generate a dot class file like Outer$Inner.class. The $ symbol represents the inner class.

When you run the above code, you will get “Error: Main method not found in class innerclass.A. This is because the outer class does not contain any main() method.

Let’s understand it with the help of an example program.

Program source code 1:

package innerClass; 
public class A 
{ 
// A member inner class named B. 
  class B 
  { 
    
  } // Inner class end. 
public static void main(String[] args)
{ 
  System.out.println("Outer class main method"); 
 } 
}
Output: 
       Outer class main method

In this program, we have declared the main() method inside the outer class. When you run this code, it will print the Outer class main method.


But what will happen when you declare the main() method inside the inner class, not outer class? Look at the source code to understand better.

Program source code 2:

package innerClass; 
public class A 
{ 
// A member inner class named B. 
  class B 
  { 
   public static void main(String[] args){ 
    System.out.println("Outer class main method"); 
   } 
  } 
}

When you will try to compile this code, it will not compile because inside the inner class, we cannot declare any static member including main() method.

Note: A member inner class cannot have a static declaration. It means that we cannot have the main method in the inner class because it is static.

How to instantiate Member Inner class in Java?


An object or instance of a member inner class always exists within an object of its outer class. The new operator is used to create the object of member inner class with slightly different syntax.

The general form of syntax to create an object of the member inner class is as follows:

Syntax:
   OuterClassReference.new MemberInnerClassConstructor();

Here, OuterClassReference is the reference of the outer class followed by a dot which is followed by the new operator.

Consider the above first example and follows the two steps.

Step 1: To create an object of inner member inner class, we must have to create first an object of its outer class.

Outer o = new Outer(); // (1)

Now we need to use the new operator on the ‘o’ object reference variable to create the object of a member inner class.

Outer.Inner i = o.new Inner(); // (2)

Where,

i is an object reference variable to store the member inner class object.
Inner() is a constructor name that is the same as the simple class name for member inner class.

When we combine the above two lines of code, we can write the following new statement.

Outer.Inner i = new Outer.new Inner(); // One line of code.

Suppose we are invoking any method like m1() using object reference variable i.

i.m1(); // Calling m1 method. // (3)

On combining (1), (2), and (3), we will get the following statement.
     new Outer().new Inner().m1();

Now consider the following class declaration with inner classes nested at multiple levels.

public class A {
  public class B {
     public class C {
        public class D {
             . . . . . . . . 
        }
     }
  }
}

How will you create an object of class D for the above lines of code?

To create an object of D, you must have an object of C. To create an object of C, you must have an object of B. Similarly, to create an object of B, you must have an object of A. Therefore, you must start by creating an object of A to create an object of D.


Key point:

To create an object of a member inner class, you first create an object of its immediate enclosing i.e outer class. The following snippet of code is given below to create an object of D:

A a = new  A();
  A.B b = a.new  B();
    A.B.C c = b.new  C();
      A.B.C.D d = c.new  D();

Let’s take different example programs to understand all concepts based on java member inner class.

Accessing Inner class code from Static area of Outer class


We can access members of an inner class from the static area of the outer class. Let’s see an example program based on it.

Program source code 3:

package innerClass; 
public class A 
{ 
// A member inner class named B. 
  class B 
  { 
   public void m1()
   { 
    System.out.println("Inner class method"); 
   } 
  } 
public static void main(String[] args)
{ 
// Static area of the outer class. 
   System.out.println("Outer class main method"); 

// Create an instance of class A. 
     A a = new A(); 
// Create an instance of class B and call method m1() from the static area of outer class using reference variable i. 
     A.B b = a.new B(); 
      b.m1(); 
   } 
}
Output: 
       Outer class main method 
       Inner class method

Accessing Inner class code from Instance area of Outer class


Let’s take an example program where we will call members of the inner class from the instance area of outer class.

Program source code 5:

package innerClass; 
public class A 
{ 
 class B 
 { 
  public void m1() { 
    System.out.println("Inner class method"); 
 } 
} 
// An instance area of outer class. 
  void m2(){ // Instance method of outer class. 
    System.out.println("Outer class instance method"); 
  // To call method m1 of an inner class B, here we create directly an object of inner class in the instance area. 
      B b = new B(); 
       b.m1(); 
  } 
public static void main(String[] args)
{ 
// Create an instance of class A. 
     A a = new A(); 
      a.m2(); 
  } 
}
Output: 
       Outer class instance method 
       Inner class method

Explanation:

In the above program, we have created an object of A and called m1() method using reference variable a. The control of execution will be immediately transferred to the m2() method that is an instanced area of the outer class A’s method m2().

Here, we have created an object of B directly and called the method m2() using reference variable b.

Member inner class in Java

Accessing Inner class code from outside of outer class


Program source code 6:

package innerClass; 
public class A
{ 
  class B 
  { 
    public void m1(){ 
       System.out.println("Inner class method"); 
     } 
   } 
// An instance area of outer class. 
    void m2(){ 
        System.out.println("Outer class instance method"); 
     } 
} 
package innerClass; 
public class Test { 
public static void main(String[] args) { 
    A a = new A(); 
    A.B b = a.new B(); 
     a.m2(); 
     b.m1(); 
   } 
}
Output: 
       Outer class instance method 
       Inner class method

Accessing Instance & Static variables of Outer class from Inner class


Program source code 7:

package innerClass; 
public class MyOuter 
{ 
// Declare instance variables and static variable. 
    int p = 10; 
    protected int q = 20; 
    private int r = 30; 
    static int s = 40; 
// Regular Inner class starts here. 
   class MyInner { 
     public void display(){
        System.out.println("Value of p: " +p); 
        System.out.println("Value of q: " +q); 
        System.out.println("Value of r: " +r); 
        System.out.println("Value of s: " +s); 
        } 
      } 
public static void main(String[] args) 
{ 
    new MyOuter().new MyInner().display(); 
  } 
}
Output: 
        Value of p: 10 
        Value of q: 20 
        Value of r: 30 
        Value of s: 40

As you can see in the above example programs that we can directly access both static and non-static variables including private of the outer class.

Key points:

1. An inner class cannot have the static declaration but it does mean that we cannot access the static members of the outer class inside the inner class. Declaration and accessing both are different things.

2. We can directly access both static and non-static variables such as public, default, protected, and private variables of the outer class inside the inner class.

Program source code 8:

package innerClass; 
public class MyOuter3 
{ 
  private int a = 20; 
 class MyInner3 { 
    private int b = 30; 
   private void showValue() { 
       System.out.println("Value of a: " +a); 
     } 
    } 
public void displayValue() { 
    MyInner3 i = new MyInner3(); 
    System.out.println("Value of b: " +i.b); 
      i.showValue(); 
 } 
public static void main(String[] args) 
{
   MyOuter3 o = new MyOuter3(); 
    o.displayValue(); 
  } 
}
Output: 
        Value of b: 30 
        Value of a: 201.

From the above program, it is clear that we can directly access the private instance variable and method of the outer class inside the inner class area.

By creating an object of the inner class, we can also access the private members (variables and methods) of an inner class inside the enclosing class instance area.

Key points:

1. An inner class can directly access all of variables and methods including private of its outer class but the reverse is not true.

2. The enclosing class does not have access to the members of an inner class directly. That is without creating an object of the inner class within the outer class’s instance area, you cannot access any members of an inner class.

Accessing Member Inner class having same Instance variable name as its Outer class


Program source code 9:

package innerClass; 
public class MyOuter2 
{ 
// Declare an instance variable of the outer class and initialize the value 20. 
    int x = 20; 
// A regular member inner class start here. 
    class MyInner2 { 
  // An instance variable of member inner class. 
       int x = 30; 
     public void showValue() { 
     // A local variable of member inner class. 
         int x = 40; 
         System.out.println(x); // It will print the value 40 of local variable x. 
         System.out.println(this.x); // It will print the value 30 of instance variable of member inner class because here this refers to the current inner class object. 
         System.out.println(MyOuter2.this.x); // It will print the value 20 of instance variable of outer class because here this refers to current outer class object. 
       } 
     } 

public static void main(String[] args) { 
     MyOuter2 mo = new MyOuter2(); 
     MyOuter2.MyInner2 mi = mo.new MyInner2(); 
      mi.showValue(); 
    } 
}
Output: 
       40 
       30 
       20

Key points:

1. Within an inner class, this.x always refers to the current inner class object.

2. MyOuter2.this.x refers to the current outer class object. Using this syntax, we can call the current outer class object x value.

Accessing Nesting of Inner class from Static area of Outer class


Program source code 10:

package innerClass; 
public class P 
{ 
   String name = "Ricky"; 
// Nesting of inner class starts here. 
    class Q { 
       private String name = "Deep"; 
     class R { 
        String name = "John"; 
        public void msg() { 
            System.out.println("Welcome you."); 
            System.out.println(P.this.name); 
            System.out.println(this.name); //Here, you cannot access the variable of class R using this keyword. 
         } 
       } 
      } 
public static void main(String[] args) { 
     P p = new P(); 
     P.Q q = p.new Q(); 
     P.Q.R r = q.new R(); 
      r.msg(); 
    System.out.println(q.name); 
 }
Output: 
        Welcome you. 
         Ricky 
         John 
         Deep

Hope that this tutorial has covered all important concepts related to member inner class in java with example programs. I hope that you will have understood this topic and enjoyed coding in this chapter.

Please, shares it on social networking sites for your friends.
Thanks for reading!!!
Next ⇒ Method local inner class in Java⇐ PrevNext ⇒

Leave a Comment