Last Updated: 28 May, 2022
‘this’ is a reference variable that refers to the current object/instance whose method or constructor is being invoked. You can refer to any member of the current object from within an instance method or a constructor by using this.
Example 1 : ‘this’ keyword referring current class instance variables
Output
x: 20
y: 40
Example 2 : Using this() to invoke current class constructor
Output
3. Parameterized constructor with 3 parameters called.
Values => a :10 b: 20 c: 30
2. Parameterized constructor with 2 parameters called.
1. Default Constructor called.
Example 3 : Using ‘this’ keyword to call current class method
Output
A - show() method
A - display() method
Example 4 : ‘this’ keyword as an argument in constructor calling
Output
Second Class => print() method called
Example 5 : Passing ‘this’ keyword as a method parameter
Output
x = 10 y = 20
Example 6 : Returning ‘this’ keyword as a current class instance
Output
x => 10 y => 20
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com