Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Core%20Java%20Interview%20Questions%20and%20Answers

Question: Can you call one constructor from another if a class has multiple constructors?
Answer:
Yes. Use this( ) syntax.
Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked. You can use this anywhere a reference to an object of the current class type is permitted.
To better understand what this refers to, consider the following version of Box( ):
Box(double w, double h, double d) { 
	this.width = w;
	this.height = h;
	this.depth = d;
}
The use of this is redundant, but perfectly correct. Inside Box( ), this will always refer to the invoking object.
Is it helpful? Yes No

Most helpful rated by users:

©2024 WithoutBook