Friday, July 1, 2011

Can you call one constructor from another?

Yes, by using this() syntax.
E.g. 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
}

No comments:

Post a Comment