A non-static class that is declared inside a class but outside the method is known as member inner class in Java or regular inner class. It can be declared as public, default, private, and protected.

The member inner class in Java can be declared like this:

Syntax:

class Outer

{

// A member inner class named Inner.

class Inner

{

. . . . . . . .

}

}

When you 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 take an example program to understand the concepts better.

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?



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.

Key point:

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



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

Outer o = new Outer(); // (1) Now, you 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 you combine the above two lines of code, you can write the following new statement.

Outer.Inner i = new Outer.new Inner(); // One line of code. Suppose you are calling any method like m1() using object reference variable i.

i.m1(); // Calling m1 method. // (3) On combining (1), (2), and (3), you 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 in the above lines of code?