Can a java subclass's private final field be initialized before the super constructor completes?

前端 未结 3 1776
感动是毒
感动是毒 2021-01-23 03:58

I have a pair of classes looking like this;

public abstract class Class1 {

//...

    public Class1() {
        //...
        function2();
        //...
    }

         


        
3条回答
  •  太阳男子
    2021-01-23 04:56

    super classes are always initialised before subclasses. Only static fields of a sub class can be initialised first.

    You can call an overridden method from the super class which then accesses a field which is not initialised. This is considered bad practice.

提交回复
热议问题