Can I call default constructor from parameterized constructor inside public class in java?

后端 未结 6 1347
暗喜
暗喜 2021-02-19 22:44

I want to call default constructor from a parameterized constructor inside a public java class.

Can I achieve it?

6条回答
  •  旧时难觅i
    2021-02-19 23:03

    In Java, the default constructor is the no-argument constructor that's implicitly provided by the compiler. And the compiler won't provide one in case you introduce any constructor with arguments.

    In that case you have to explicitly define a no-argument constructor (which is not default by the way, because it's not provided by the compiler), e.g. public MyClass() { }.

    And you can call it from other constructor as this();, which must be the first statement in the constructor where it's being called.

提交回复
热议问题