Open In App

How to Access Inner Classes in Java?

Improve
Improve
Like Article
Like
Save
Share
Report

In Java, inner class refers to the class that is declared inside class or interface which were mainly introduced, to sum up, same logically relatable classes as Java is purely object-oriented so bringing it closer to the real world. It is suggested to have adequate knowledge access the inner class, first create an object of the outer class after that create an object of the inner class. As the inner class exists inside the outer class we must instantiate the outer class in order to instantiate the inner class. Hence, to access the inner class, first create an object of the outer class after that create an object of the inner class.

So as we know there are multiple types of inner classes been up there in Java  

Example 1:

Java




// Java Program to demonstrate How to Access Inner Class
 
// Importing required classes
import java.io.*;
 
// Class 1
// Outer class
class Outer {
 
    // Class 2
    // Inner class
    class Inner {
 
        // Data member defined inside inner class
        int num = 10;
    }
}
 
// Class 2
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an instance of outer class inside main()
        Outer obj1 = new Outer();
 
        // Creating an instance of inner class inside main()
        Outer.Inner obj2 = obj1.new Inner();
 
        // Accessing inner class's data member
        System.out.println(obj2.num);
    }
}


 
 

Output

10

Now let us propose another example demonstrating private inner class. As inner classes can be made private or protected, unlike “regular classes”. If an inner class is private it is impossible to access that class using an outside object.

 

Example 2:

 

Java




// Java Program to Demonstrate Private Inner Class
 
// Importing required classes
import java.io.*;
 
// Class 1
// Outer class
class Outer {
 
    // Private inner class
    private class Inner {
 
        int num = 10;
    }
}
 
// Class 2
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Outer class object
        Outer obj1 = new Outer();
 
        // Inner class object
        Outer.Inner obj2 = obj1.new Inner();
 
        // Accessing inner class's data member
        System.out.println(obj2.num);
    }
}


 

 

Output:

 

Now let us propose another example illustrating static inner class. As we know the inner class can be static. We can access static inner class without creating objects of the outer class. 

 

Example 3:

 

Java




// Java Program to Demonstrate Static Inner Class
 
// Importing required classes
import java.io.*;
 
// Class 1
// Outer class
class Outer {
 
    // Static inner class
    static class Inner {
 
        // Inner class member variable
        int num = 10;
    }
}
 
// Class 2
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Inner class object
        Outer.Inner obj2 = new Outer.Inner();
 
        // Accessing inner class's data member
        System.out.println(obj2.num);
    }
}


 
 

Output

10

 



Last Updated : 17 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads