0
public final class ImmutableList<E> { public final E head; public final ImmutableList<E> tail; public ImmutableList() { this.head = null; this.tail = null; } private ImmutableList(E head, ImmutableList<E> tail) { this.head = head; this.tail = tail; } 

I know that public final E head is declaring an attribute from the generic tip E , this syntax is familiar to me but what does this public final ImmutableList<E> tail; mean, why declaring this attribute using the name of the generic class and what's the difference between :

public final E head; 

and this :

public final ImmutableList<E> tail; 

are they similar ?

4
  • What would this mean: class A { private final A obj; }? Commented Mar 2, 2015 at 19:47
  • why not using public final E tail istead of ImmutableList<E> Tail !! Commented Mar 2, 2015 at 19:52
  • That would not form a LinkedList, which is what this code is supposed to represent I would guess. Commented Mar 2, 2015 at 19:53
  • no the goal of the code to not use List and LinkedList to improve performance Commented Mar 2, 2015 at 19:54

2 Answers 2

1

This code is a typical implementation of a recursive list. Each list has a head of type E, and as tail another recursive list which also has a head of type E, and as tail yet another recursive list.

The problem with recursion is that to understand it you have to understand recursion first.

Sign up to request clarification or add additional context in comments.

Comments

0

The difference is that the former defines exactly one object of a type E named head. The latter defines an ImmutableList containing a bunch of objects E named tail. This is a recursive definition.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.