Provide Best Programming Tutorials

Java this Keyword

In this chapter you will learn:

  1. What is Java this keyword for
  2. How to use this keyword to reference class member variables
  3. Example – use this to reference instance variable
  4. How to call other constructor


The keyword this refers to the object itself. It can also be used inside a constructor to invoke another constructor of the same class.

this refers to the current object.

this can be used inside any method to refer to the current object.


The following code shows how to use this keyword.

// A use of this.
Rectangle(double w, double h) { 
    this.width = w; // this is used here
    this.height = h; 

Hidden instance variables and this

Use this to reference the hidden instance variables.

Member variables and method parameters may have the same name. Under this situation we can use this to reference the member variables.

Rectangle(double width, double height) { 
    this.width = width; 
    this.height = height; 


The following example shows how to use this to reference instance variable.

class Person{
    private String name;

    public Person(String name) { = name;
    public String getName() {
        return name;
    public void setName(String name) { = name;
public class Main{
    public static void main(String[] args) {
        Person person = new Person("Java");
        person.setName("new name");

The code above generates the following result.

Using this to Invoke a Constructor

The this keyword can be used to invoke another constructor of the same class. For example

Leave a Reply

Close Menu