Java allows a class to be defined within another class. These are called Nested Classes. The class in which the nested class is defined is known as the Outer Class. Unlike top level classes, Inner classes can be Static. Non-static nested classes are also known as Inner classes.
An instance of an inner class cannot be created without an instance of the outer class. Therefore, an inner class instance can access all of the members of its outer class, without using a reference to the outer class instance. For this reason inner classes can help make programs simple and concise.
What are the differences between static and non-static nested classes?
The following are major differences between static nested classes and inner classes.
- A static nested class may be instantiated without instantiating its outer class.
- Inner classes can access both static and non-static members of the outer class. A static class can access only the static members of the outer class.
Message from nested static class: GeeksForGeeks Message from non-static nested class: GeeksForGeeks Message from non-static nested class: GeeksForGeeks
Reference Book: Introduction To Java Programming by Y. DANIEL LIANG
This article is contributed by Chandra Prakash. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Static and non static blank final variables in Java
- Understanding "static" in "public static void main" in Java
- Difference between static and non-static method in Java
- Difference between static and non-static variables in Java
- Understanding storage of static methods and static variables in Java
- Why non-static variable cannot be referenced from a static method in Java
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Shadowing of static functions in Java
- Are static local variables allowed in Java?
- Assigning values to static final variables in Java
- Comparison of static keyword in C++ and Java
- Static blocks in Java
- Can we Overload or Override static methods in java ?
- Static vs Dynamic Binding in Java
- Static methods vs Instance methods in Java
- static keyword in java
- Final static variable in Java
- Static import in Java
- Static Control Flow in Java
Improved By : mailjesseduncan