+ 9
In Java, 'this' keyword is a reference to the current object whose method is being called. The various usage of keyword 'this' in Java :  - It can be used to refer current class    instance variable ;  - It can be used to invoke or initiate    current class constructor ;  - It can be passed as an argument in the    method call ;  - It can be passed as argument in the    constructor call ;  - It can be used to return the current class    instance .
7th Jan 2019, 10:35 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 9
#1 this REFERENCE All instance methods receive an implicit argument called 'this' which refers to the current object. The current object is the object on which the method was called and 'this' reference can be used inside any method to refer to the current object. In the body of the method or constructor, the 'this' reference can be used like any other object reference to access instance variables, instance methods, and constructor. ...
7th Jan 2019, 10:45 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 8
This should help you. If you need more, ask here and I'll attempt to fill in the missing details. https://www.dummies.com/programming/java/reference-types-in-java/
7th Jan 2019, 10:09 PM
John Wells
John Wells - avatar
+ 8
#2 The main situation where the 'this' reference can be used are: ‱ When you set the name of the parameters different from the name of the instance variables inside the constructor or method, in this case, we can use 'this' reference to access the instance variables and you must use the following syntax: this.instance_variable_name = instance_variable_name; ‱ When you need to pass a reference to the current object as an argument to another method. ‱ When you need to call a constructor from another constructor. You can use this() syntax. The argument to this() must match with target constructor and this() must be the first line inside the constructor. ➝ By using this(), the duplicate code can be avoided in multiple constructors, especially when the initialization routine is complex. https://code.sololearn.com/cU86021A34uj/?ref=app
7th Jan 2019, 10:46 PM
Danijel Ivanović
Danijel Ivanović - avatar