Can we call one constructor from another Constructor?
>> Sunday, May 29, 2011
Yes ,We can call one constructor from another Constructor by using this.() method.
Look the below Example:
Look the below Example:
class Pet
{
int id=9;
String type="check";
public Pet(int id) {
this.id = id; // “this” means this object
}
public Pet (int id, String type) {
this(id); // calls constructor public Pet(int id)
this.type = type; // ”this” means this object
}
public static void main(String[] args)
{
Pet p=new Pet(9,"check");
}
}
0 comments:
Post a Comment