Java Interview Question
Use this intermediate question and answer to prepare for your next Java interview.
Explain the uses of
this is a self-reference to the current instance of the class. It can be used to invoke the constructor, to reference a class-level variable or method, or to refer to or pass the object instance.
this is used for calling the constructor within another constructor, it is called constructor chaining. It must be the first line of the constructor call.
this is also used for variable disambiguation. When there is a local variable masking a class-level variable, because they have the same name, then you can refer to the class-level variable by using
this and you can refer to the local variable directly by the name.