I was looking at the Java code for LinkedList
and noticed that it made use of a static nested class, Entry
.
public class LinkedList<E> ... {
...
private static class Entry<E> { ... }
}
What is the reason for using a static nested class, rather than an normal inner class?
The only reason I could think of, was that Entry doesn't have access to instance variables, so from an OOP point of view it has better encapsulation.
But I thought there might be other reasons, maybe performance. What might it be?
Note. I hope I have got my terms correct, I would have called it a static inner class, but I think this is wrong: http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
One of the reasons for static vs. normal have to do with classloading. You cannot instantiate an inner class in the constructor of it's parent.
PS: I've always understood 'nested' and 'inner' to be interchangeable. There may be subtle nuances in the terms but most Java developers would understand either.
The Sun page you link to has some key differences between the two:
There is no need for
LinkedList.Entry
to be top-level class as it is only used byLinkedList
(there are some other interfaces that also have static nested classes namedEntry
, such asMap.Entry
- same concept). And since it does not need access to LinkedList's members, it makes sense for it to be static - it's a much cleaner approach.As Jon Skeet points out, I think it is a better idea if you are using a nested class is to start off with it being static, and then decide if it really needs to be non-static based on your usage.
Simple example :
If non-static the class cannot be instantiated exept in an instance of the upper class (so not in the example where main is a static function)
Adavantage of inner class--
Without existing of outer class inner class will not exist.
There are four types of inner class.
point ---
inorder to invoke normal inner class in static area of outer class.
Outer 0=new Outer(); Outer.Inner i= O.new Inner();
inorder to invoke normal inner class in instance area of outer class.
Inner i=new Inner();
inorder to invoke normal inner class in outside of outer class.
Outer 0=new Outer(); Outer.Inner i= O.new Inner();
inside Inner class This pointer to inner class.
this.member-current inner class outerclassname.this--outer class
for inner class applicable modifier is -- public,default,
final,abstract,strictfp,+private,protected,static
outer$inner is the name of inner class name.
inner class inside instance method then we can acess static and instance field of outer class.
10.inner class inside static method then we can access only static field of
outer class.
Static inner class is used in the builder pattern. Static inner class can instantiate it's outer class which has only private constructor. So you can use static inner class to instantiate the outer class which only has private constructor. You can not do the same with the inner class as you need to have object of the outer class created prior to accessing the inner class.
This will output x: 1
Using a static nested class rather than non-static one may save spaces in some cases. For example: implementing a
Comparator
inside a class, say Student.Then the
static
ensures that the Student class has only one Comparator, rather than instantiate a new one every time a new student instance is created.