What is ‘this’ reference in Java?

In Java, 'this' is a keyword that is used to refer to the current object within a class or instance method. It can be used to reference instance variables, methods, and constructors of the current object.

When a method is called on an object, 'this' is implicitly passed as an argument to the method. This allows the method to access the object's instance variables and methods. For example, consider the following class:

public class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }
}

In the constructor of the Person class, 'this' is used to refer to the instance variable 'name', to distinguish it from the parameter 'name'. In the getName() method, 'this' is used to explicitly refer to the instance variable 'name'.

In addition to referencing instance variables and methods, 'this' can also be used to call one constructor from another constructor within the same class. This is known as constructor chaining. For example:

public class Person {
    private String name;
    private int age;

    public Person(String name) {
        this(name, 0);
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

In this example, the first constructor calls the second constructor using 'this', passing in a default value of 0 for the 'age' parameter.

Overall, 'this' is a useful keyword in Java that allows for easy and unambiguous reference to the current object within a class or instance method.

‘this’ may be a reference variable that refers to the present object.
Following are the ways to use the ‘this’ keyword in java :

1. Using ‘this’ keyword to refer to current class instance variables

//Java code for using 'this' keyword to
//refer current class instance variables
class Test
{
	int a;
	int b;
	
	// Parameterized constructor
	Test(int a, int b)
	{
		this.a = a;
		this.b = b;
	}

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

	public static void main(String[] args)
	{
		Test object = new Test(10, 20);
		object.display();
	}
}

Output:

a = 10  b = 20

2. Using this() to invoke the current class constructor

JAVA

// Java code for using this() to
// invoke current class constructor
class Test
{
	int a;
	int b;

	//Default constructor
	Test()
	{
		this(10, 20);
		System.out.println("Inside default constructor \n");
	}
	
	//Parameterized constructor
	Test(int a, int b)
	{
		this.a = a;
		this.b = b;
		System.out.println("Inside parameterized constructor");
	}

	public static void main(String[] args)
	{
		Test object = new Test();
	}
}

Output:

Inside parameterized constructor
Inside  default constructor

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

JAVA

//Java code for using 'this' keyword
//to return the current class instance
class Test
{
	int a;
	int b;

	//Default constructor
	Test()
	{
		a = 10;
		b = 20;
	}
	
	//Method that returns current class instance
	Test 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)
	{
		Test object = new Test();
		object.get().display();
	}
}

Output:

a = 10  b = 20

4. Using ‘this’ keyword as a method parameter

// Java code for using 'this'
// keyword as method parameter
class Test
{
	int a;
	int b;
	
	// Default constructor
	Test()
	{
		a = 10;
		b = 20;
	}
	
	// Method that receives 'this' keyword as parameter
	void display(Test obj)
	{
		System.out.println("a = " +obj.a + " b = " + obj.b);
	}

	// Method that returns current class instance
	void get()
	{
		display(this);
	}

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

Output:

a = 10  b = 20

5. Using ‘this’ keyword to invoke the current class method

// Java code for using this to invoke current
// class method
class Test {

	void display()
	{
		// calling function show()
		this.show();
	
	System.out.println("Inside display function");
	}
	
	void show() {
		System.out.println("Inside show funcion");
	}
	

	public static void main(String args[]) {
		Test t1 = new Test();
		t1.display();
	}
}

Output : 

Inside show funcion
Inside display function

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

// Java code for using this as an argument in constructor
// call
// Class with object of Class B as its data member
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();
	}
	
}

class B
{
	int x = 5;
	
	// Default Constructor 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();
	}
}

Output : 

Value of x in Class B : 5

 

Submit Your Programming Assignment Details