In this post, I will list out possible uses of these two operators. Before that let me give brief details
- These are keywords having special semantic/meaning in language.
- Most uses are accessing the members or invoking the constructor.
- Uses of are valid within- Constructor, Instance initializer block, Instance initializer expression. Use in constructs than these are invalid and compiler will throw error accordingly.
Uses of this
It is operator which represents the value which is reference to object to which instance method is invoked (in case of method or block scope) or object in the construction (constructor scope)
In simple words, when using this operator you have access to invoking object, to which you can access any instance member on that.
Access the members
You may modify access to members using this operator to explicitly saying that accessing the member of that class. Without modifying it will access only member which belongs to same class
Basic syntax is this.<member>
Following snippet shows usage
Following program shows the this reference value and Shape reference variable, to which method is invoked, are same
Above code makes clears that this is the reference to invoking object
Access member variable in case parameter name is same as field name
There may be the case that you are having same name to parameter as that of field (mostly in constructor parameter) for readability purpose. Lets look at following code snippet
This will not result into assignment to field because instance field is hidden by local variable as it is in local block scope.
To override the local variable scope, we should make use this operator as shown following code
Invoke the another constructor
It may be case that you want to create new constructor which is doing same as that of another constructor but with something different and you don’t want to duplicate code the constructor code
We can have this , using syntax : this ( <param> )
Following code shows usage
Remember, the invocation must be very first statement in the constructor. If it is not then such attempt is invalid.
Use of super
This operator comes into use when class has inheritance and want to access the member of the super class in subclass which are accessible.
Access the hidden member variable
When in a sub class, you are having the variable with same name as that of in super class then super class variable is hidden in the subclass. Therefore, the any access to that variable in subclass will result in to one in the subclass.
So, to access the hidden variable from super class we have to use super as shown in following code snippet.
Access the overridden method from super class
This same as accessing the variable as above
lets look at following code snippet
Invoking the super class constructor
Like invoking another constructor of same class as in this(), you can invoke super class constructor. Using syntax , super(<param>)
Lets look at following code snippet
When you write any constructor, having statements except for this(), then compiler always puts the call to super class default constructor which is super().
Once last point, When using super or this for constructor invocation one of them must be used.