Java - 为什么在对象构造期间我的final字段为null?

时间:2015-11-25 04:28:30

标签: java inheritance nullpointerexception

我有两个类,一个是抽象类,另一个类是扩展抽象类。

public abstract class ClassA {

    private Object object;

    public ClassA(Object object){
        this.object = object;
        initialize();
    }

    protected abstract void initialize();

}

public class ClassB extends ClassA {

    private final List<Object> objectList = new ArrayList<>();

    public ClassB(Object object){
        super(object);
    }

    @Override
    protected void initialize(){
        objectList.clear();
        //objectList is showing null here when I call .clear() on it!
        //How is this possible?

        for(Object object : objectList){
            NPE!
        }

    }

}

我不明白上面示例中的ClassB.objectList为什么/如何给我一个NullPointerException。

0 个答案:

没有答案