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

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows