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

What is this keyword in java?

568


Do you know how to reverse string in java?

585


What are the rules for variable declaration?

507


difference between byte stream class and character stream class?

4087


what is aggregation in java?

588






What is numel matlab?

661


Difference between stack and queue?

590


What is the purpose of default constructor?

584


How to instantiate static nested classes in java?

566


What do you mean by chromounits in java8?

528


What is operator overloading. Is it is supported in java?

524


What are the various access specifiers in java?

569


What are inner classes or non static nested classes in java?

707


How to handle a web browser resize operation?

545


Can we synchronize static methods in java?

601