How do I call one constructor from another in Java?

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

    Calling constructor from another constructor

    class MyConstructorDemo extends ConstructorDemo
    {
        MyConstructorDemo()
        {
            this("calling another constructor");
        }
        MyConstructorDemo(String arg)
        {
            System.out.print("This is passed String by another constructor :"+arg);
        }
    }
    

    Also you can call parent constructor by using super() call

提交回复
热议问题