Thursday, 12 February 2015

this() can be used to invoked current class constructor

  1. class Student13{  
  2.     int id;  
  3.     String name;  
  4.     Student13(){System.out.println("default constructor is invoked");}  
  5.       
  6.     Student13(int id,String name){  
  7.     this ();//it is used to invoked current class constructor.  
  8.     this.id = id;  
  9.     this.name = name;  
  10.     }  
  11.     void display(){System.out.println(id+" "+name);}  
  12.       
  13.     public static void main(String args[]){  
  14.     Student13 e1 = new Student13(111,"karan");  
  15.     Student13 e2 = new Student13(222,"Aryan");  
  16.     e1.display();  
  17.     e2.display();  
  18.    }  

No comments:

Post a Comment