Nested inner class or member inner class

As we have read in recent post java nested class has two types, static nested class and non- static nested class(Java Inner class). The nested inner class or member inner class( java member class) is part of the non-static nested class (Java Inner class). In this post, we will learn member inner class and private class in java.

Here is the table content of the article will we will cover this topic.
1. What is nested inner class or member inner class?
2. How to instantiate Member Inner class in java?
3. How to access members of the Inner class in Outer class?
4. How to access members of the Outer class in Inner class?
5. Accessing the static members of Outer Class?

6. Can we use static keyword in member inner class?
7. What is a private inner class?
8. Internal working by the compiler?

What is nested inner class or member inner class(java member class)?

As we know the non-static nested classes are also known as inner classes in java. The nested inner class is the simplest form of inner class in Java. It is also known as member inner class because it declares with the class as a member of class. The member inner class always outside the method and inside the class. A member inner class can be declared with any access modifier like private, protected, public, and default.
It is the only way when we can create a class as private and protected. We will see how we can create a private class in java.

java member class

Let’s take another example

public class OuterClass 
{
    // Method of outer class
	void methodOfOuter()
	{
		System.out.println("Outer class");
	}
    
	// Creating member inner class 
	class InnerClass 
	{
        // Method of member inner class
	    public void methodOfInner() 
        {
			System.out.println("Inner class");
		}
	}
	public static void main(String[] args) 
       { 
		//Body of the main method
       }
}

How to instantiate Member Inner class(java member class) in java?

We can’t create an object or instance of a member inner class directly. The object/instance of a member inner class totally depends on the object of the outer class. Unlike normal class, the nested class has a different way to create an object or instance.

java member class

To create an object of member inner class we need to create an object of Outer class first. Because the scope of member inner class is enclosed in Outer class.

OuterClass objOfOuter = new OuterClass();
InnerClass objOfInner = objOfOuter.new InnerClass();
public class OuterClass 
{
    // Method of outer class
	void methodOfOuter()
	{
		System.out.println("Outer class");
	}
    
	// Creating member inner class 
	class InnerClass
	{
        // Method of member inner class
	    public void methodOfInner() 
        {
			System.out.println("Inner class");
		}
	}
	public static void main(String[] args) 
    { 
		OuterClass objOfOuter = new OuterClass();
		InnerClass objOfInner = objOfOuter.new InnerClass();
		objOfOuter.methodOfOuter();
		objOfInner.methodOfInner();
    }
}

Output: Outer class
Inner class

How to access members of the Inner class in Outer class?

We can access the member of the Inner class in Outer class by the object of Inner class. We can access the member of the Inner class in a static method as well as in the non-static method(Instance method).

In static method can access the member of the Inner class only when we create an object of Inner class through the object of Outer class. But in the instance method(non-static method) we can directly create object of the Inner class. Let’s take an example and see how we can access it.

public class OuterClass 
{
    // Creating member inner class 
	class InnerClass
	{
		int a = 5;
        // Method of member inner class
	    public void methodOfInner() 
        {
			System.out.println("Inner class");
		}
	}
        public static void main(String[] args) 
        { 
		OuterClass objOfOuter = new OuterClass();
		InnerClass objOfInner = objOfOuter.new InnerClass();
		System.out.println("Value of a = " +objOfInner.a);
		objOfInner.methodOfInner();
		
		objOfOuter.showData();
        }
	
	void showData()
	{
		InnerClass objOfInner = new InnerClass();
		System.out.println("Show data method and value of a : "+ objOfInner.a);
		objOfInner.methodOfInner();
	}
}

Output: Value of a = 5
Inner class
Show data method and value of a : 5
Inner class

java member class

How to access members of the Outer class in Inner class(java member class)?

We can access each and every member of the Outer class in the Inner class. Even they are private or not. This is the only way to access the private member of a class.

public class OuterClass 
{
    private int a = 1; // private variable
    protected int b = 2; // protected variable
    public int c = 3; // public variable
    int d = 4; // default variable
	
    // Creating member inner class 
	class InnerClass
	{
        // Method of member inner class
	    public void methodOfInner() 
        {
			System.out.println("Accessing private variable : "+ a);
			System.out.println("Accessing protected variable : "+ b);
			System.out.println("Accessing public variable : "+ c);
			System.out.println("Accessing default variable : "+ d);
		}
	}
        public static void main(String[] args) 
        { 
		OuterClass objOfOuter = new OuterClass();
		InnerClass objOfInner = objOfOuter.new InnerClass();
		
		objOfInner.methodOfInner();
		
        }
}

Output: Accessing private variable : 1
Accessing protected variable : 2
Accessing public variable : 3
Accessing default variable : 4

Accessing the static members of Outer Class

We can access the static member of the Outer class. Although we can’t use static keyword in Inner class, we can access the static member of the outer class. We don’t need to create an object of Outer class to access the static members.

public class OuterClass 
{
	static int a = 5;
	static void shwoData()
	{
		System.out.println("Static method Outer class");
	}
	
    // Creating member inner class 
	class InnerClass
	{
	    public void methodOfInner() 
        {
			System.out.println("Static variable of Outer class a :"+ a);
			shwoData();
		}
	}
	public static void main(String[] args) 
    { 
		OuterClass objOfOuter = new OuterClass();
		InnerClass objOfInner = objOfOuter.new InnerClass();
		objOfInner.methodOfInner();
    }
}

Output: Static variable of Outer class a :5
Static method Outer class

Can we use static keyword in member inner class?

We can’t use the static keyword in the member inner class. If we use the static keyword in the member inner class only with the final keyword. It means we can make then it compiler will show compilation error. Let’s see it with an example

public class OuterClass 
{
	static int a = 5;
	static void methodOfOuterClass()
	{
		System.out.println("static method of Outer class");
	}
    // Creating member inner class 
	class InnerClass
	{
		static int b = 10;
        // Method of member inner class
	    static void methodOfInner() 
        {
			System.out.println("static method of Inner class");
		}
	}
        public static void main(String[] args) 
        { 
		OuterClass objOfOuter = new OuterClass();
		InnerClass objOfInner = objOfOuter.new InnerClass();
		
		objOfOuter.methodOfOuterClass();
		objOfInner.methodOfInner();		
        }
}

Output: Compilation error

What is a private inner class?

We know a class cannot be associated with the access modifier private, but if we have the class as a member of other classes, then the inner class can be made private.

class OuterClassName
{
	// variables and methods of Outer Class
	private class InnerClassName
        {
		// variables and methods of Inner class
        }
}
class Data 
{  
    // variable of Data class
    int variableOfData = 5;
	
    // inner nested class 
    private class Record 
    { 
    	// Non static method of Student class
        void display() 
        { 
        	// You can access the variables of Data class.
            System.out.println("You can access the variable ="+variableOfData);
        } 
    } 
    
    // Method of Data class
    void methodOfData()
    {
    	Record record = new Record();
    	record.display();
    }
} 
  
public class MainClass 
{ 
    public static void main(String[] args) 
    { 	
    	Data data = new Data();
    	data.methodOfData();
    } 
}

Output: You can access the variable =5

Internal working by the compiler

The compiler internal works differently in the case of inner classes. The compiler creates two class files in the case of an inner class. One class file for Outer class and another for Inner class. The class file name of the inner class is “Outer$Inner”. If you want to create an object of Inner class, you must have to create the object of the Outer class.

Leave a Comment