Sunday, February 25, 2018

Java program : Constructor calling a Constructor

class CtorCallingCtor
{
    CtorCallingCtor()
    {   
        this(11);                             //To call ctor inside ctor we use 'this' keyword
        System.out.println("0 arg ctor");     // The 'this' statement MUST be the FIRST statement in the ctor
                                            // One ctor can call only ONE ctor in this way   
    }
   
    CtorCallingCtor(int m)
    {
        //this(); //CTE RECURSIVE CTOR INVOCATION NOT ALLOwED
        System.out.println("1 arg ctor  " + m);
    }

   
    public static void main(String[] args)
    {
        new CtorCallingCtor();
        new CtorCallingCtor(12);
    }   
}   

Thanks
Happy programming!

No comments:

Post a Comment

Sacred Thought

26 April 2024  Dear friends, I write the explanation of two verses of Geets for all of you, I hope you all will like it and benefit from it....