class EmptyListException extends RuntimeException {} public class List { private final Object head; private final List tail; private List(Object head, List tail) { this.head = head; this.tail = tail; } public static List nil() { return new List(null, null); } private boolean isNil() { return tail == null; } public Object head() { if (isNil()) throw new EmptyListException(); return head; } public List tail() { if (isNil()) throw new EmptyListException(); return tail; } public List add(Object o) { return new List(o, this); } public static char char1(List l) { return ((String) l.head()).charAt(0); } public static void main(String[] args) { List ls = List.nil().add("Banana"); System.out.println(char1(ls)); List li = List.nil().add(1); System.out.println(char1(li)); } }