How do I call one constructor from another in Java?

前端 未结 21 2485
不知归路
不知归路 2020-11-22 01:06

Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how? And what could be the best way to call another constructor (if th

21条回答
  •  你的背包
    2020-11-22 01:14

    You can call another constructor via the this(...) keyword (when you need to call a constructor from the same class) or the super(...) keyword (when you need to call a constructor from a superclass).

    However, such a call must be the first statement of your constructor. To overcome this limitation, use this answer.

提交回复
热议问题