1

Linklist have static class named as ENTRY

private static class Entry<E> {
E element;
Entry<E> next;
Entry<E> previous;

Entry(E paramE, Entry<E> paramEntry1, Entry<E> paramEntry2){
  this.element = paramE;
  this.next = paramEntry1;
  this.previous = paramEntry2;
}
}

And an object has created inside LinkList

private transient Entry<E> header = new Entry(null, null, null);

Following are my questions?

  1. Why it is static ?
  2. If we define more than two LinkList will it share the same static class 'Entry'?
Alok Pathak
  • 875
  • 1
  • 8
  • 20

1 Answers1

3

In terms of performance, non-static classes contain an implicit reference to the outer class which created them. If you don't need that reference, you can save memory by making it static. (See JLS 8.1.3: Inner classes whose declarations do not occur in a static context may freely refer to the instance variables of their enclosing class. This ability requires storing a reference to that class.)

In terms of semantics, it's clearer to readers of the code that the inner class contains no dependency on the outer class if you make it static. From this point of view, you should always declare your inner classes static until you reach a point where you require a dependency on the enclosing class.

In regards to your question 2: I'm assuming you mean "if we define more than two instances of a LinkedList". They will share the same static class (that is, the LinkedList.Entry.class object itself) but will, of course, contains different Entry instances.

Chris Hayes
  • 11,471
  • 4
  • 32
  • 47