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:
- How could Java classes direct program messages to the system console, but error messages, say to a file?
- What are the differences between an interface and an abstract class?
- Why would you use a synchronized block vs. synchronized method?
- How can you force garbage collection?
- What are the differences between the methods sleep() and wait()?