This Keyword Interview Questions in Java
Interview Questions on This Keyword in Java
What is this keyword ?
this is reference variable that refers to the current object.
Why use this keyword ?
this keyword can be used to refer current class instance variable and this keyword can also be used to return the current class instance.
What are the uses of this keyword in constructor ?
this can be passed as argument in the constructor call.
Can we call methods using this keyword ?
Yes we can use this keyword to call current class non static methods.
Uses of this keyword with constructor ?
Used to invoke current class constructor.
Difference Between this() and super() ?
Super keyword is always pointing to base class features and this keyword is always pointing to current class features.
this is a reference to the current object in which this keyword is used whereas super is a reference used to access members specific to the parent Class.
this is primarily used for accessing member variables if local variables have same name, for constructor chaining and for passing itself to some method whereas super is primarily used to initialize base class members within derived class constructor.
What is the difference between this. (this dot) and this() (this off). ?
this. can be used to differentiate variable of class and formal parameters of method or constructor. this() can be used to call one constructor within the another constructor without creation of objects multiple time for the same class.
What is difference between super(), super(..), this() and this(..) ?
super() and super(..) are used for establishing the communication between base class and derived class constructor.
this() and this(...) are used for establishing the communication between current class constructor.