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

后端 未结 6 1332
暗喜
暗喜 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条回答
  •  情书的邮戳
    2021-02-19 23:11

    For Java: You might mean the constructor without parameters. If so you can use the following code:

    public class MyClass {
       // no params constructor 
       public MyClass() {
          ...
       }
    
       // parametrized constructor
       public MyClass(int p1, String p2) {
           this();
       }
    }
    

    Hope this helps

提交回复
热议问题