When does the Constructor gets called in java?

前端 未结 10 1417
[愿得一人]
[愿得一人] 2020-12-16 02:14

When does the Constructor get called?

  1. Before object creation.
  2. During object creation.
  3. After object creation.
10条回答
  •  被撕碎了的回忆
    2020-12-16 03:13

    It gets called at object creation. The memory must be reserved first for the object, otherwise the constructor code could not run. So maybe we could say after object creation. Also note that initialization code written in the class gets called before the constructor code.

    public Ex {
    
        int xVal = -1;
        int yVal;
    
        public Ex() {
            // xVal is already -1.
            //yVal defaults to 0.
        }
    }
    

提交回复
热议问题