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

package com.gs.ilp.corejava.thisKeyword;

public class TestThis {
	int variable = 5;

	void method(int variable) {
		variable = 10;
		System.out.println("Value of variable :" + variable);
	}

	void method() {
		int variable = 40;
		System.out.println("Value of variable :" + variable);
	}

	public static void main(String args[]) {
		TestThis obj = new TestThis();
		obj.method(20);
		obj.method();
	}
}

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

package com.gs.ilp.corejava.thisKeyword;

public class TestWithThis {
	int variable = 5;

	public static void main(String args[]) {
		TestWithThis obj = new TestWithThis();
		obj.method(20);
		obj.method();
	}

	void method(int variable) {
		variable = 10;
		System.out.println("Value of Instance variable :" + this.variable);
		System.out.println("Value of Local variable :" + variable);
	}

	void method() {
		int variable = 40;
		System.out.println("Value of Instance variable :" + this.variable);
		System.out.println("Value of Local variable :" + variable);
	}
}

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

package com.gs.ilp.corejava.thisKeyword;

public class TestWithConstructor {
	TestWithConstructor() {
		this("Test ");
		System.out.println("Inside Constructor without parameter");
	}

	TestWithConstructor(String str) {
		System.out.println("Inside Constructor with String parameter as " + str);
	}

	public static void main(String[] args) {
		TestWithConstructor obj = new TestWithConstructor();
	}
}

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

package com.gs.ilp.corejava.thisKeyword;

public class TestThisWithMethod {
	public static void main(String[] args) {
		TestThisWithMethod obj = new TestThisWithMethod();
		obj.methodTwo();
	}

	void methodOne() {
		System.out.println("Inside Method ONE");
	}

	void methodTwo() {
		System.out.println("Inside Method TWO");
		this.methodOne();// same as calling methodOne()
	}
}

Output of above program

Inside Method TWO

Inside Method ONE

3. this keyword as Method parameter

package com.gs.ilp.corejava.thisKeyword;

public class ParentTest {
	public static void main(String[] args) {
		ABC obj = new ABC();
		obj.i = 10;
		obj.method();
	}
}

class ABC extends ParentTest {
	int i;

	void method() {
		method1(this);
	}

	void method1(ABC t) {
		System.out.println(t.i);
	}
}

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

package com.gs.ilp.corejava.thisKeyword;

public class TestCurrInstance {
	int a;
	int b;

	// Default constructor
	TestCurrInstance() {
		a = 10;
		b = 20;
	}

	// Method that returns current class instance
	TestCurrInstance get() {
		return this;
	}

	// Displaying value of variables a and b
	void display() {
		System.out.println("a = " + a + "  b = " + b);
	}

	public static void main(String[] args) {
		TestCurrInstance object = new TestCurrInstance();
		object.get().display();
	}
}

Output:

a = 10 b = 20

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

package com.gs.ilp.corejava.thisKeyword;

class A {
	B obj;

	// Parameterized constructor with object of B
	// as a parameter
	A(B obj) {
		this.obj = obj;
		// calling display method of class B
		obj.display();
	}
}

public class B {
	int x = 5;

	// Default Contructor that create a object of A
	// with passing this as an argument in the
	// constructor
	B() {
		A obj = new A(this);
	}

	// method to show value of x
	void display() {
		System.out.println("Value of x in Class B : " + x);
	}

	public static void main(String[] args) {
		B obj = new B();
	}
}

Note*:

  • this keyword can only be the first statement in Constructor.

  • A constructor can have either this or super keyword but not both.

Last updated