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

Class IX IT notes: Part A, Unit 1, Session 3.

 IT notes class 9 Part A Unit 1: COMMUNICATION SKILLS session 3: VISUAL AND WRITTEN METHODS OF COMMUNICATION Q1. What is Visual Communicatio...