类的默认构造函数和变量初始化

时间:2017-09-20 19:51:43

标签: java constructor default-constructor

所以我有一个没有构造函数的类,我想知道默认构造如何初始化值。将elementWithCachedMax初始化为新的LinkedList吗?怎么样,为什么?我在哪里可以了解有关默认构造函数如何初始化成员变量的更多信息?

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())));
    }
}

0 个答案:

没有答案