Can you call a constructor within a constructor?

Answer Posted / qim2010

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
}

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Should a main method be compulsorily declared in all java classes?

520


What are exception handling keywords in java?

599


When we should use serialization?

554


Can you explain inner class.

583


What checkbox method allows you to tell if a checkbox is checked?

529






What are the Static and Dynamic Variables? Differentiate them.

592


Does java have a compiler?

551


Why is java so important?

639


When should you make a function static?

537


What do you understand by the term wrapper classes?

534


What is the use of private static?

524


Why are the destructors for base class and derived class called in reverse order when the program exits

1694


What is a qms manual?

535


How do you override a private method in java?

490


What does sprintf mean?

523