C
C
Core Java
Search…
17) this keyword

What is this

"this" is a keyword in Java.
"this" is a predefined instance variable to hold current object reference. 'this' is also called as “Default Reference variable” of java.
It can be used inside the Method or constructor of Class. It (this) works as a reference to the current Object whose Method or constructor is being invoked. this keyword can be used to refer to any member of the current object from within an instance Method or a constructor.

Rule to develop 'this':

    this, should always be the first statement inside a constructor.
    We cannot develop multiple this, inside a single constructor.
    this, is used to call only constructor of the same class.
    If we want to call constructor from another constructor we should use this. We cannot use constructor name because we can use the constructor name only at the time of object creation.

this calling statement:

'this' is used to call one constructor from another constructor of the same class in case of Constructor Overloading.
Generally for 1 object creation, only 1 constructor shall be executed. But if we want to execute multiple constructor of the same class for only one object creation we can use this() calling statement.
this (parameter) will call the constructor of the same class with matching parameters.

Advantage:

If we can't to use the initialization code written is another constructor of the same class, then we make use of this.
Following are the ways to use ‘this’ keyword in java:
    1.
    Using ‘this’ keyword to refer current class instance variables
    2.
    Using this() to invoke current class constructor
    3.
    Using ‘this’ keyword to invoke current class method
    4.
    Using ‘this’ keyword as method parameter
    5.
    Using ‘this’ keyword to return the current class instance
    6.
    Using ‘this’ keyword as an argument in the constructor call
    7.
    this keyword with field(Instance Variable)
this keyword can be very useful in the handling of Variable Hiding. We cannot create two instance/local variables with the same name. However it is legal to create one instance variable & one local variable or Method parameter with the same name. In this scenario the local variable will hide the instance variable this is called Variable Hiding.

Example of Variable Hiding

1
package com.gs.ilp.corejava.thisKeyword;
2
3
public class TestThis {
4
int variable = 5;
5
6
void method(int variable) {
7
variable = 10;
8
System.out.println("Value of variable :" + variable);
9
}
10
11
void method() {
12
int variable = 40;
13
System.out.println("Value of variable :" + variable);
14
}
15
16
public static void main(String args[]) {
17
TestThis obj = new TestThis();
18
obj.method(20);
19
obj.method();
20
}
21
}
Copied!

Output of the above program

Value of variable :10
Value of variable :40
As you can see in the example above the instance variable is hiding and the value of the local variable (or Method Parameter) is displayed not instance variable. To solve this problem use this keyword with a field to point to the instance variable instead of the local variable.

Example of this keyword in Java for Variable Hiding

1
package com.gs.ilp.corejava.thisKeyword;
2
3
public class TestWithThis {
4
int variable = 5;
5
6
public static void main(String args[]) {
7
TestWithThis obj = new TestWithThis();
8
obj.method(20);
9
obj.method();
10
}
11
12
void method(int variable) {
13
variable = 10;
14
System.out.println("Value of Instance variable :" + this.variable);
15
System.out.println("Value of Local variable :" + variable);
16
}
17
18
void method() {
19
int variable = 40;
20
System.out.println("Value of Instance variable :" + this.variable);
21
System.out.println("Value of Local variable :" + variable);
22
}
23
}
Copied!

Output of the above program

Value of Instance variable :5
Value of Local variable :10
Value of Instance variable :5
Value of Local variable :40

1. this Keyword with Constructor

“this” keyword can be used inside the constructor to call another overloaded constructor in the same Class. This is called the Explicit Constructor Invocation. This occurs if a Class has two overloaded constructors, one without argument and another with argument. Then the “this” keyword can be used to call constructor with argument from the constructor without argument. This is required as the constructor cannot be called explicitly.

Example of this with Constructor

1
package com.gs.ilp.corejava.thisKeyword;
2
3
public class TestWithConstructor {
4
TestWithConstructor() {
5
this("Test ");
6
System.out.println("Inside Constructor without parameter");
7
}
8
9
TestWithConstructor(String str) {
10
System.out.println("Inside Constructor with String parameter as " + str);
11
}
12
13
public static void main(String[] args) {
14
TestWithConstructor obj = new TestWithConstructor();
15
}
16
}
Copied!

Output of above program

Inside Constructor with String parameter as Test
Inside Constructor without parameter
As you can see “this” can be used to invoke an overloaded constructor in the same Class.

2. this Keyword with Method

this keyword can also be used inside Methods to call another Method from same Class.

Example of this keyword with Method

1
package com.gs.ilp.corejava.thisKeyword;
2
3
public class TestThisWithMethod {
4
public static void main(String[] args) {
5
TestThisWithMethod obj = new TestThisWithMethod();
6
obj.methodTwo();
7
}
8
9
void methodOne() {
10
System.out.println("Inside Method ONE");
11
}
12
13
void methodTwo() {
14
System.out.println("Inside Method TWO");
15
this.methodOne();// same as calling methodOne()
16
}
17
}
Copied!

Output of above program

Inside Method TWO
Inside Method ONE

3. this keyword as Method parameter

1
package com.gs.ilp.corejava.thisKeyword;
2
3
public class ParentTest {
4
public static void main(String[] args) {
5
ABC obj = new ABC();
6
obj.i = 10;
7
obj.method();
8
}
9
}
10
11
class ABC extends ParentTest {
12
int i;
13
14
void method() {
15
method1(this);
16
}
17
18
void method1(ABC t) {
19
System.out.println(t.i);
20
}
21
}
Copied!

4. Using ‘this’ keyword to return the current class instance

1
package com.gs.ilp.corejava.thisKeyword;
2
3
public class TestCurrInstance {
4
int a;
5
int b;
6
7
// Default constructor
8
TestCurrInstance() {
9
a = 10;
10
b = 20;
11
}
12
13
// Method that returns current class instance
14
TestCurrInstance get() {
15
return this;
16
}
17
18
// Displaying value of variables a and b
19
void display() {
20
System.out.println("a = " + a + " b = " + b);
21
}
22
23
public static void main(String[] args) {
24
TestCurrInstance object = new TestCurrInstance();
25
object.get().display();
26
}
27
}
Copied!
Output:
a = 10 b = 20

5. Using ‘this’ keyword as an argument in the constructor call

1
package com.gs.ilp.corejava.thisKeyword;
2
3
class A {
4
B obj;
5
6
// Parameterized constructor with object of B
7
// as a parameter
8
A(B obj) {
9
this.obj = obj;
10
// calling display method of class B
11
obj.display();
12
}
13
}
14
15
public class B {
16
int x = 5;
17
18
// Default Contructor that create a object of A
19
// with passing this as an argument in the
20
// constructor
21
B() {
22
A obj = new A(this);
23
}
24
25
// method to show value of x
26
void display() {
27
System.out.println("Value of x in Class B : " + x);
28
}
29
30
public static void main(String[] args) {
31
B obj = new B();
32
}
33
}
Copied!
Note*:
    this keyword can only be the first statement in Constructor.
    A constructor can have either this or super keyword but not both.
Last modified 2yr ago