How do I call one constructor from another in Java?

前端 未结 21 2525
不知归路
不知归路 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:22

    As everybody already have said, you use this(…), which is called an explicit constructor invocation.

    However, keep in mind that within such an explicit constructor invocation statement you may not refer to

    • any instance variables or
    • any instance methods or
    • any inner classes declared in this class or any superclass, or
    • this or
    • super.

    As stated in JLS (§8.8.7.1).

提交回复
热议问题