Nested class - it is a class within a class. Even though it is a class, this nested class becomes a member of the outer class just like the fields, constructors and methods. Since it is a member of the enclosing class, this nested class can have a modifier of public, private, protected or package private. Unlike the enclosing class which has public and package private only.
Two types
1. non static nested class or inner class - This inner class even if declared private can access the members of the enclosing class. The object of an inner class does exist within the object of an outer class. Check out in the main section of the example below on how to access the object of the inner class.
=========================
class OuterClass{
private String name;
public OuterClass(String name){
this.name = name;
}
private class InnerClass{
void displayName(){
System.out.println("name: " + name);
}
}
public static void main(String[] args){
OuterClass outerObject = new OuterClass("Bell Cranel");
OuterClass.InnerClass innerObj = outerObj.new InnerClass();
innerObj.displayName()
}
}
========================
Result:
name: Bell Cranel
2. static nested class - Static nested class cannot access the non static member of the enclosing class directly. You need to use a reference to access that non static member. However the static member can be accessed directly. The instance of this static nested class is treated like an outsider class or a top level class. You can create the object of this static nested class normally. Check the example below. Take note of the static and non static variable and how they were access.
=============================
class OuterClass {
String instanceOuterField = "Instance outer member variable";
static String staticOuterField = "Static outer member variable";
void displayDashLines(){
System.out.println("---------------------------");
}
private class InnerClass{
void displayHereInInstance(){
System.out.println("Below is the inner class");
displayDashLines();
System.out.println(instanceOuterField);
System.out.println(staticOuterField);
System.out.println("*************************");
}
}
private static class StaticNestedClass{
void displayHereInStatic(OuterClass obj){
System.out.println("Below is the static nested class");
obj.displayDashLines();
System.out.println(obj.instanceOuterField);
System.out.println(staticOuterField);
System.out.println("************************");
}
}
public static void main(String[] args){
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
innerObject.displayHereInInstance();
StaticNestedClass staticNestedClass = new StaticNestedClass();
staticNestedClass.displayHereInStatic(outerObject);
TopLevelOuterClass topOuterObject = new TopLevelOuterClass();
topOuterObject.displayHereInTopLevelClass(outerObject);
}
}
class TopLevelOuterClass{
void displayHereInTopLevelClass(OuterClass obj){
System.out.println("Below is the top level outer class");
obj.displayDashLines();
System.out.println(obj.instanceOuterField);
System.out.println(OuterClass.staticOuterField);
System.out.println("************************");
}
}===============================Result:Below is the inner class---------------------------Instance outer member variableStatic outer member variable*************************
Below is the static nested class---------------------------Instance outer member variableStatic outer member variable************************
Below is the top level outer class---------------------------Instance outer member variableStatic outer member variable************************
Shadowing in inner class - when it is in a normal class, we used this keyword to refer to act as an instance of the class and then connect it with a dot operator followed by the field. However the inner field is shadowing the outer field of the enclosing class as well. So, you need to use the class name of the enclosing class connect it with a dot operator followed by this keyword and connect it with another dot operator followed by the outer field variable. Check the implementation in the example below for clarity.
=========================
class OuterClass {
private int x = 0;
private class InnerClass{
private int x = 1;
void displayX(int x){
System.out.println("Variables are shadowing each other!");
System.out.println("method parameter: " + x);
System.out.println("inner class field: " + this.x);
System.out.println("outer class field: " + OuterClass.this.x);
}
}public static void main(String[] args){
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
innerObject.displayX(2);
}
}==========================
Result:
Variables are shadowing each other!method parameter: 2inner class field: 1outer class field: 0
Note:
Serialization of inner classes is discouraged because it creates a synthetic constructs. Synthetic construct will create new java language features that causes compatibility issues in the .class file for it varies in different java compilers. Serialization will be discuss of the succeeding chapters.
YOU ARE READING
Java Programming
Randomhttps://docs.oracle.com/javase/tutorial/ This is the link where I learned my java lessons online. I choose java as first programming language. I am a career shifter. I just want to start something new. I choose to write it here in Wattpad so that I...