The scope of the nested class is defined within the curly bracket. In java, it is possible to define the class within another class; such classes are known as nested classes.
Content Overview
Nested Class enable you to logically group classes that are only used in one place; thus, it increases the use of encapsulation, and create more readable and maintainable code.
See the following syntax.
class outer { // statements of outer class class inner { // statements of inner class } } // outer close
A non-static nested class is also called as an inner class in which the inner class has all the rights to access the member of the outer class. The inner class can access the private, protected or default member of the outer class, but the reverse is not possible, i.e. an outer class cannot access the member of the inner class. The inner class is always created within the scope of the outer class.
It is again divided into two categories:
class outer { private int a = 1; // private memberstatic int b = 2; // static member
int c = 3; // non-static member
class inner {
void show() {
System.out.println(“private member of outer class=” + a);System.out.println("static member of outer class=" + b); System.out.println("non-static member of outer class=" + c); } // method close
} // inner class close
} // outer class closeclass nested {
public static void main(String args[]) {
outer obj = new outer();
outer.inner obj1 = obj.new inner();
obj1.show();
}
}
See the output.
A static nested class is a class that is defined within another class with a static modifier with the class name. As the class is defined with static modifier so it can access only the static member of the outer class but not the non-static member.
As with class methods and variables, the static nested class is associated with its outer class and like static class methods, the static nested class cannot refer directly to the instance variables or methods defined in its enclosing class. It can use them only through the object reference.
The static inner class is the nested class, which is the static member of the outer class. It can be accessed without instantiating an outer class, using other static members. Just like static members, the static nested class does not have access to the instance variables and methods of the outer class.
Outer classname.inner classname object_name=new outer classname.inner classname( );
class outer {
private static int a = 1;static int b = 2;
int c = 3;
static class inner {
void show() {
System.out.println(“private member of outer class=” + a);
System.out.println(“static member of outer class=” + b);
// System.out.println(“non-static member of outer class=”+c); //this statement
// is not ligal as the innerclass is static and outerclass is non-static
}
}
}class static_nested {
public static void main(String args[]) {
outer.inner obj = new outer.inner();
obj.show();
}
}
See the output.
Finally, Nested class In Java Example | Java Nested Class Tutorial is over.
Originally published by Ankit Lathiya at appdividend.com
=====================================================================
Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter
☞ Java Programming Masterclass for Software Developers
☞ JSP, Servlets and JDBC for Beginners: Build a Database App
☞ Java 8 New Features In Simple Way
☞ Java In-Depth: Become a Complete Java Engineer!
☞ Java Programming for Complete Beginners - Learn in 250 Steps
☞ Learn Java Programming Crash Course
☞ Apache Spark for Java Developers
#java