What is the main difference between an inner class and a static nested class in Java? Does design / implementation play a role in choosing one of these?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
When we declare static member class inside a class, it is known as top level nested class or a static nested class. It can be demonstrated as below :
When we declare non-static member class inside a class it is known as inner class. Inner class can be demonstrated as below :
In simple terms we need nested classes primarily because Java does not provide closures.
Nested Classes are classes defined inside the body of another enclosing class. They are of two types - static and non-static.
They are treated as members of the enclosing class, hence you can specify any of the four access specifiers -
private, package, protected, public
. We don't have this luxury with top-level classes, which can only be declaredpublic
or package-private.Inner classes aka Non-stack classes have access to other members of the top class, even if they are declared private while Static nested classes do not have access to other members of the top class.
Inner1
is our static inner class andInner2
is our inner class which is not static. The key difference between them, you can't create anInner2
instance without an Outer where as you can create anInner1
object independently.When would you use Inner class?
Think of a situation where
Class A
andClass B
are related,Class B
needs to accessClass A
members, andClass B
is related only toClass A
. Inner classes comes into the picture.For creating an instance of inner class, you need to create an instance of your outer class.
or
When would you use static Inner class?
You would define a static inner class when you know that it does not have any relationship with the instance of the enclosing class/top class. If your inner class doesn't use methods or fields of the outer class, it's just a waste of space, so make it static.
For example, to create an object for the static nested class, use this syntax:
The advantage of a static nested class is that it doesn't need an object of the containing class/top class to work. This can help you to reduce the number of objects your application creates at runtime.
Nested class is a very general term: every class which is not top level is a nested class. An inner class is a non-static nested class. Joseph Darcy wrote a very nice explanation about Nested, Inner, Member, and Top-Level Classes.
From the Java Tutorial:
Static nested classes are accessed using the enclosing class name:
For example, to create an object for the static nested class, use this syntax:
Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:
An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
see: Java Tutorial - Nested Classes
For completeness note that there is also such a thing as an inner class without an enclosing instance:
Here,
new A() { ... }
is an inner class defined in a static context and does not have an enclosing instance.The terms are used interchangeably. If you want to be really pedantic about it, then you could define "nested class" to refer to a static inner class, one which has no enclosing instance. In code, you might have something like this:
That's not really a widely accepted definition though.
Ummm... an inner class IS a nested class... do you mean anonymous class and inner class?
Edit: If you actually meant inner vs anonymous... an inner class is just a class defined within a class such as:
Whereas an anonymous class is an extension of a class defined anonymously, so no actual "class is defined, as in:
Further Edit:
Wikipedia claims there is a difference in Java, but I've been working with Java for 8 years, and it's the first I heard such a distinction... not to mention there are no references there to back up the claim... bottom line, an inner class is a class defined within a class (static or not), and nested is just another term to mean the same thing.
There is a subtle difference between static and non-static nested class... basically non-static inner classes have implicit access to instance fields and methods of the enclosing class (thus they cannot be constructed in a static context, it will be a compiler error). Static nested classes, on the other hand, don't have implicit access to instance fields and methods, and CAN be constructed in a static context.