Member Inner Class in Java with Example

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 a regular inner class. It can be declared with access modifiers like public, default, private, and protected.

Syntax to Declare Member Inner Class in Java


The syntax to declare a member inner class in Java is:

class Outer
{
  // Outer class body
  // A member inner class named Inner.
    class Inner  
    {
      // Inner class body
    }
}

When we compile the class Outer like this:

javac Outer.java

The compiler will generate two separate dot class files:

  • Outer.class for the outer class
  • Outer$Inner.class for the inner class

This is because the class Inner is not a direct class. It is present somewhere inside the 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.

Example 1: Member Inner Class

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 example, 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 the outer class? Look at the example code to understand better.

Example 2: main Method Inside Inner Class

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

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

Note:

  • Prior to Java 16, a member inner class could not declare static members except constant variables.
  • Since Java 16, static members and static methods (including main()) are allowed inside member inner classes. Therefore, a member inner class can now contain a main() method in modern Java versions.

How to Instantiate a 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 the member inner class with slightly different syntax.

Syntax to Create Object of Member Inner Class

The general syntax to create an object of the member inner class in Java is:

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 example and follow the two steps.

Step 1: Create an Object of Outer Class

A member inner class object is associated with an object of its enclosing outer class. To create an object of a member inner class, we must first create an object of its outer class.

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

Step 2: Create an Object of Member Inner Class

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 a 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.

Calling a Method

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

i.m1(); // Calling m1 method. // (3)
// By combining (1), (2), and (3), we get the following statement.
new Outer().new Inner().m1();

In this example:

  • We have created an object of outer class.
  • Then, we have created an Inner object associated with that Outer object.
  • After that, we have called the m1() method.

Nested Inner Classes in Java


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?

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

To create an object of a member inner class, you first create an object of its immediate enclosing 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 classes.

Examples of Member Inner Classes in Java


Example 3: Accessing Members of Inner Class from Static Method

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

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.
      A.B b = a.new B(); 
   // Call method m1() from the static area of outer class using reference variable i. 
      b.m1(); 
  } 
}

Output:

Outer class main method 
Inner class method

Since main() is a static method, it cannot directly create an object of the non-static inner class B. To create an object of the inner class, first, create an object of the outer class A. After the creation of outer class, create an object of the inner class.

Example 5: Accessing Members of Inner Class from Instance Method

Let’s take an example program in which we will call members of the inner class from the instance method of the outer class.

public class A 
{ 
  class B 
  { 
    public void m1() { 
       System.out.println("Inner class method"); 
    } 
  } 
// Instance method of an outer class. 
   void m2() { 
      System.out.println("Outer class instance method"); 
  
   // To call method m1 of an inner class B, we create an object of inner class inside it. 
      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

In this example program:

  • We have created an object of class A and called the m1() method using reference variable a.
  • When a.m2() is called, control transfers to the instance method m2() of class A.
  • Inside an instance method of the outer class, Java already has access to the current outer object (this). Therefore, the compiler automatically treats “B b = new B();” as “B b = this.new B();.” Hence, no explicit outer-class object is required.
  • Inside the instance method, we have created an object of inner class B and called its method m1() using reference variable b.

Accessing member inner class from different area of outer class in Java.

Example 6: Accessing Members of an Inner Class from Outside of Outer Class

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"); 
     } 
} 
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

Example 7: Accessing Instance & Static variables of Outer Class from Inner Class

Let us consider an example where we access instance variables and static variables of an outer class inside the inner class.

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, we can directly access both static and non-static variables, including private ones of the outer class.

Important Note

  • A member inner class in Java can directly access all members of its enclosing outer class, including private, protected, default, public, static, and non-static members.
  • An object of a member inner class is always associated with an object of its enclosing outer class.

Example 8: Accessing Private Members of Outer Class Inside Inner Class

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() { 
            System.out.println("Value of b: " + b); 
            showValue(); 
       } 
       public static void main(String[] args) { 
           MyOuter3 outer = new MyOuter3(); 
           MyOuter3.MyInner3 inner = outer.new MyInner3(); inner.displayValue(); 
       } 
    } 
}

Output:

Value of b: 30 
Value of a: 20

In this example:

  • a is a private instance variable of the outer class MyOuter3.
  • b is a private instance variable of the inner class MyInner3.
  • The method showValue() is a private method of the inner class.
  • The inner class directly accesses the private variable a of the outer class.
  • Since Java allows an inner class to access all members of its enclosing class, including private members, no getter method is required.
  • In Java 16 and later, a member inner class can contain static members, including the main() method.

Important Note

  • An inner class can directly access all members of its outer class, including private members.
  • Similarly, the outer class can access private members of the inner class through an inner-class object.
  • 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.

Example 9: Variable Shadowing in Member Inner Class

public class MyOuter2 
{ 
// Declare an instance variable and assign the value 20 to it. 
   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; 
     // This statement prints the value 40 of local variable x.
        System.out.println(x); 
     // This statement prints the value 30 of instance variable of member inner class.
     // Because the keyword this refers to the current inner class object. 
        System.out.println(this.x);
     // This statement prints the value 20 of instance variable of outer class.
     // Because the keyword this refers to current outer class object. 
        System.out.println(MyOuter2.this.x);      
     } 
   } 
   public static void main(String[] args) { 
       MyOuter2 mo = new MyOuter2(); 
       MyOuter2.MyInner2 mi = mo.new MyInner2(); 
       mi.showValue(); 
   } 
}

Output:

40 
30 
20

In this example:

  • Within a member inner class, this.x refers to the instance variable x of the current inner class object.
  • MyOuter2.this.x refers to the instance variable x of the current enclosing outer class object.
  • This syntax is used when an inner class needs to access a hidden or shadowed member of its outer class.
  • When a local variable, an inner-class variable, and an outer-class variable have the same name, Java resolves them in the following order:
    • Local variable (x)
    • Inner class instance variable (this.x)
    • Outer class instance variable (OuterClassName.this.x)

Example 10: Accessing Outer and Inner Class Variables in Nested Inner Classes

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 all."); 
          System.out.println(P.this.name); 
          System.out.println(Q.this.name);
          System.out.println(this.name); 
        } 
      } 
   } 
   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 all. 
Ricky 
Deep
John

In this example:

  • A member inner class can contain another inner class. This is known as nested inner classes in Java.
  • The innermost class can directly access the members of all its enclosing classes.
  • The this.name refers to the instance variable of the current class R.
  • The Q.this.name refers to the instance variable of the immediate enclosing inner class Q.
  • The P.this.name refers to the instance variable of the outermost enclosing class P.
  • When multiple classes contain variables with the same name, Java allows us to access them using this and OuterClassName.this syntax.
  • To create an object of a nested inner class, you must first create objects of all its enclosing classes.
P p = new P();
P.Q q = p.new Q();
P.Q.R r = q.new R();
  • The expression p.new Q() creates an object of inner class Q associated with the object p of class P.
  • The expression q.new R() creates an object of inner class R associated with the object q of class Q.
  • Nested inner classes are useful when a class is logically related only to its enclosing class and is not intended to be used independently.
  • Java resolves variables with the same name in the following order:
    • Local variable
    • Current class variable (this.variableName)
    • Enclosing class variable (OuterClassName.this.variableName)
  • The syntax OuterClassName.this is used to access members of a specific enclosing class when they are hidden by members of inner classes.
DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.