Inner Classes In Java

We will explore Java inner classes with examples in today’s topic to meet learners’ needs more effectively.

Java Inner Classes

Java also supports nesting classes (classes within classes). With Java inner classes, nesting is used to group related classes, which improves code readability and maintainability.

Creating an outer class object and then creating an inner class object is the initial step for accessing the inner class:

Example: 

class OuterClass{String OuterClass_bio="This is an Outer Class Attribute";class InnerClass{String InnerClass_bio="This is an Inner Class Attribute";}}public class Main { public static void main(String[] args) {OuterClass my_OuterClass_Obj=new OuterClass(); OuterClass.InnerClass my_InnerClass_Obj=my_OuterClass_Obj.new InnerClass();System.out.println("OuterClass value: "+my_OuterClass_Obj.OuterClass_bio); System.out.println("InnerClass value: "+my_InnerClass_Obj.InnerClass_bio);}// Output 1: OuterClass value: This is an Outer Class Attribute // Output 2: InnerClass value: This is an Inner Class Attribute }

Another Example: 

class Outer_Class{int mrx_outer=3;class Inner_Class{int ample_inner=13;}}public class Main { public static void main(String[] args) {Outer_Class my_OuterClass_Obj=new Outer_Class(); Outer_Class.Inner_Class my_InnerClass_Obj=my_OuterClass_Obj.new Inner_Class(); System.out.println("Product of Inner and Outer class attribute: "+(my_OuterClass_Obj.mrx_outer*my_InnerClass_Obj.ample_inner));}// Output : Product of Inner and Outer class attribute: 39}


Private Inner Class

Java Inner classes can be private or protected, unlike regular classes.

Declare the inner class as private if you do not want outside objects to access it:

If you try to access a private inner class from an outside class (MyMainClass), an error occurs:

Example: 

class Outer_Class{int ample=40; private class Inner_Class{int mrx=20;} }public class Main { public static void main(String[] args) { Outer_Class my_Obj1=new Outer_Class(); Outer_Class.Inner_Class my_Obj2=my_Obj1.new Inner_Class();System.out.println("Difference: "+(my_Obj1.ample- my_Obj2.mrx)); } }

 

java: Outer_Class.Inner_class has private access in Outer_Class
Outer_Class.Inner_Class my_Obj2 = my_Obj1.new Inner_Class();

Another Example: 

class Outer_Class{String mrx_name="James Gosling"; private class Inner_Class{int ample_age=67;} }public class Main { public static void main(String[] args) { Outer_Class my_Obj1=new Outer_Class(); Outer_Class.Inner_Class my_Obj2=my_Obj1.new Inner_Class();System.out.println("Name: "+my_Obj1.mrx_name+"\nAge: "+my_Obj2.ample_age); } }
java: Outer_Class.Inner_Class has private access in Outer_Class

Static Inner Class

In Java inner class, you can also access it without creating an object of the outer class:

Example: 

class O_Class{int mrx=16;static class I_Class{int ample=6; } }public class Main { public static void main(String[] args) { O_Class.I_Class obj1=new O_Class.I_Class(); System.out.println("Attribute value from Inner static class: "+(obj1.ample)); } // Output: Attribute value from Inner static class: 6 }

 

Another Example: 

class O_Class{String mrx="John";static class I_Class{String ample="Robert"; } }public class Main { public static void main(String[] args) { O_Class.I_Class obj1=new O_Class.I_Class(); System.out.println("Output: "+(obj1.ample)); } // Output: Robert }

 

Remember : A static inner class has no access to members of the outer class, just as static attributes and methods do.


Using Inner Class to Access Outer

Java Inner classes have the advantage of accessing methods and attributes of their outer classes:

Example: 

class Out_class{int mrx=3*4;class In_class{public int return_value(){ return mrx; } } }public class Main { public static void main(String[] args) {Out_class my_Object1=new Out_class(); Out_class.In_class my_Object2=my_Object1.new In_class();System.out.println(my_Object2.return_value()); } // Output: 12 (Inner Class object is used here to access outer class method) }

 

Another Example: 

class Out_class{int mrx=14; int ample=3;class In_class{public int return_modulus(){ return mrx%ample; } } }public class Main { public static void main(String[] args) {Out_class my_Object1=new Out_class(); Out_class.In_class my_Object2=my_Object1.new In_class();System.out.println(my_Object2.return_modulus()); } // Output: 2 (Inner Class object is used here to access outer class method as 14 % 3 =2 ) }
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *