Java this keyword

The this keyword refers to the current object instance. It is typically used within a class to refer to the current instance variables and methods. In this tutorial, we will discuss the basics of the this keyword in Java with a code example.

Syntax of this Keyword:

The this keyword is used to refer to the current object instance. Here is the syntax of the this keyword:

class ClassName {
    int instanceVariable;
    
    void methodName(int parameter) {
        this.instanceVariable = parameter;
    }
}

In the above example, we have declared an instance variable instanceVariable and a method methodName that takes an integer parameter parameter. We use the this keyword to refer to the current object instance and set the value of instanceVariable to parameter.

Using this Keyword to Avoid Naming Conflicts:

One common use of the this keyword is to avoid naming conflicts between instance variables and local variables within a method. Here is an example:

/**
 * This Keyword Example to Avoid Naming Conflicts
 * 
 * @author Mamun Kayum
 *
 */
public class ThisKeywordExAvoidNamingConflicts {
	int instanceVariable;

	public ThisKeywordExAvoidNamingConflicts(int instanceVariable) {
		this.instanceVariable = instanceVariable;
	}
	
	void methodName(int instanceVariable) {
		this.instanceVariable = instanceVariable;
	}
	
	public static void main(String[] args) {
		ThisKeywordExAvoidNamingConflicts avoidNamingConflicts = new ThisKeywordExAvoidNamingConflicts(10);
		System.out.println("instanceVariable value: " + avoidNamingConflicts.instanceVariable);
	}
}

In the above example, we have declared an instance variable instanceVariable and a method methodName that takes an integer parameter also named instanceVariable. To avoid the naming conflict, we use the this keyword to refer to the instance variable instanceVariable and set its value to the parameter instanceVariable.

Using this Keyword to Call a Constructor:

Another use of the this keyword is to call a constructor from another constructor within the same class. Here is an example:

/**
 * This Keyword example to call a Constructor
 * @author Mamun Kayum
 *
 */
public class ThisKeywordCallAConstructor {
    int instanceVariable;

    ThisKeywordCallAConstructor(int instanceVariable) {
        this.instanceVariable = instanceVariable;
    }

    ThisKeywordCallAConstructor() {
        this(30); // calls the other constructor with 0 as the argument
    }
    
    public static void main(String[] args) {
    	// initializing ThisKeywordCallAConstructor class with no argument constructor
    	ThisKeywordCallAConstructor callAConstructor = new ThisKeywordCallAConstructor();
    	
    	System.out.println("instanceVariable value: "+callAConstructor.instanceVariable);
	}
}

In the above example, we have two constructors for the ThisKeywordCallAConstructor class. The first constructor takes an integer parameter instanceVariable and sets the value of the instance variable instanceVariable. The second constructor calls the first constructor with 0 as the argument using the this keyword.

Using this Keyword to Return the Current Object:

The this keyword can also be used to return the current object instance from a method. Here is an example:

/**
 * This as return current object example
 * 
 * @author Mamun Kayum
 *
 */
public class ThisAsReturnCurrentObject {
	int instanceVariable = 10;

	ThisAsReturnCurrentObject setVal(int instanceVariable) {
		this.instanceVariable = instanceVariable;
		return this;
	}

	public int getVal() {
		return instanceVariable;
	}
}

In the above example, we have a method setVal that takes an integer parameter instanceVariable and sets the value of the instance variable instanceVariable. The method then returns the current object instance using the this keyword.

/**
 * This as return current object caller
 * 
 * @author Mamun Kayum
 *
 */
public class ThisAsReturnCurrentObjectCaller {
	public static void main(String[] args) {
		ThisAsReturnCurrentObject asReturnCurrentObject = new ThisAsReturnCurrentObject();

		// setting value and getting the instance of the current object
		ThisAsReturnCurrentObject currentObject = asReturnCurrentObject.setVal(100);

		System.out.println("Value is: " + currentObject.getVal());
	}
}

Caller class is setting value and getting the instance of the current object and then finally using that object to call the “getVal()” method to print the current value.

Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author