CONSTRUCTOR EXAMPLE IN JAVA

 class Test  
 {  
      int a,b;  
      Test()  
      {  
           this(10); //calling current class single parameterized constructor  
           System.out.println("I'm from defalult constructor.");  
           a=1;  
           b=2;  
           System.out.println("value of A: "+a);  
           System.out.println("value of B: "+b);  
      }  
      Test(int x)  
      {  
           this(100,200); //calling current class double parameterized constructor  
           System.out.println("I'm from Single parameterized constructor.");  
           a=b=x;  
           System.out.println("value of A: "+a);  
           System.out.println("value of B: "+b);  
      }  
      Test(int a,int b)  
      {  
           System.out.println("I'm from double parameterized constructor.");  
           this.a=a;  
           this.b=b;  
           System.out.println("value of this A: "+this.a);  
           System.out.println("value of this B: "+this.b);  
           System.out.println("value of A: "+a);  
           System.out.println("value of B: "+b);  
      }  
 }  
 class ConstructDemo   
 {  
      public static void main(String[] args)   
      {  
           Test obj=new Test();  
      }  
 }  

Post a Comment

0 Comments