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

Derivatives stock list at NSE

Complete FNO stock list at NSE. ABB India Ltd ACC Ltd APL Apollo Tubes Ltd AU Small Finance Bank Ltd Aarti Industries Ltd Abbott India Ltd A...