So I have a class without a constructor and I was wondering how the values are initialized by the default construct. Will elementWithCachedMax be initialized to new LinkedList? How and Why? Where can I learn more about how the default constructor initializes member variables?
public static class Stack {
// Stores (element, cached maximum) pair.
private Deque<ElementWithCachedMax> elementWithCachedMax
= new LinkedList<>();
public boolean empty() { return elementWithCachedMax.isEmpty(); }
public Integer max() {
if (empty()) {
throw new IllegalStateException("max(): empty stack");
}
return elementWithCachedMax.peek().max;
}
public Integer pop() {
if (empty()) {
throw new IllegalStateException("pop(): empty stack");
}
return elementWithCachedMax.removeFirst().element;
}
public static void print(){
System.out.println("HI");
}
public void push(Integer x) {
elementWithCachedMax.addFirst(
new ElementWithCachedMax(x, Math.max(x, empty() ? x : max())));
}
}