Home » This keyword in Java

This keyword in Java

This keyword in Java

What is the purpose of the “this” keyword in Java?

The this keyword is used to represent the current instance of a class. It is used to access the instance variables and invoke current class methods and constructors. This keyword can be passed as an argument to a method call representing the current class instance. This is the reserved keyword in Java that can be used to invoke the constructors, methods, static members, etc. of the current instance of a class.

Examining the Java This Keyword Usage in More Detail

‘Java This Keyword’ comes of great use in various situations. One of those is when it’s used within an instance method or a constructor, this keyword is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this keyword.

Usage of Java this keyword

• It can be used to refer to the current class instance variables.

Example

public class MyClass {
    private int x;
    private int y;

    // Constructor initializing instance variables using 'this'
    public MyClass(int x, int y) {
        this.x = x; // 'this' refers to the instance variable 'x'
        this.y = y; // 'this' refers to the instance variable 'y'
    }

    // Method using 'this' to access instance variables
    public void setX(int x) {
        this.x = x; // 'this' refers to the instance variable 'x'
    }

    public void setY(int y) {
        this.y = y; // 'this' refers to the instance variable 'y'
    }

    public int getX() {
        return this.x; // 'this' is optional here, used for clarity
    }

    public int getY() {
        return this.y; // 'this' is optional here, used for clarity
    }

    // Main method to demonstrate usage
    public static void main(String[] args) {
        MyClass obj = new MyClass(5, 10);
        System.out.println("Initial values: x = " + obj.getX() + ", y = " + obj.getY());
        
        obj.setX(20);
        obj.setY(30);
        
        System.out.println("Updated values: x = " + obj.getX() + ", y = " + obj.getY());
    }
}
Java

Output

Initial values: x = 5, y = 10
Updated values: x = 20, y = 30
Java

• It can be passed as an argument in the method call representing the current object of the class.

Example

public class MyClass {
    private int x;
    private int y;

    // Constructor initializing instance variables using 'this'
    public MyClass(int x, int y) {
        this.x = x; // 'this' refers to the instance variable 'x'
        this.y = y; // 'this' refers to the instance variable 'y'
    }

    // Method using 'this' to pass the current object as an argument
    public void doSomething() {
        System.out.println("Doing something with x = " + this.x + " and y = " + this.y);
        doSomethingElse(this); // Passes the current object as a parameter
    }

    public void doSomethingElse(MyClass obj) {
        // Accessing obj's properties
        System.out.println("Doing something else with x = " + obj.x + " and y = " + obj.y);
    }

    // Main method to demonstrate usage
    public static void main(String[] args) {
        MyClass obj = new MyClass(5, 10);
        obj.doSomething(); // Invokes doSomething method passing the current object
    }
}
Java

Output

Doing something with x = 5 and y = 10
Doing something else with x = 5 and y = 10
Java

• It can be used to return the current class instance from any of its instance methods.

Example

public class MyClass {
    private int x;
    private int y;

    // Constructor initializing instance variables using 'this'
    public MyClass(int x, int y) {
        this.x = x; // 'this' refers to the instance variable 'x'
        this.y = y; // 'this' refers to the instance variable 'y'
    }

    // Method using 'this' to return the current class instance
    public MyClass updateX(int newX) {
        this.x = newX; // Update x with the new value
        return this; // Return the current class instance
    }

    public MyClass updateY(int newY) {
        this.y = newY; // Update y with the new value
        return this; // Return the current class instance
    }

    // Method to display the current values of x and y
    public void displayValues() {
        System.out.println("Current values: x = " + this.x + ", y = " + this.y);
    }

    // Main method to demonstrate usage
    public static void main(String[] args) {
        MyClass obj = new MyClass(5, 10);
        
        // Update x and y using method chaining
        obj.updateX(20).updateY(30).displayValues();
    }
}
Java

Output

Current values: x = 20, y = 30
Java

• It can be used to access instance and static variables of the current instance.

Example

public class MyClass {
    private int instanceVariable;
    private static int staticVariable;

    // Constructor initializing instance variables using 'this'
    public MyClass(int instanceVariable) {
        this.instanceVariable = instanceVariable; // 'this' refers to the instance variable 'instanceVariable'
    }

    // Method accessing both instance and static variables
    public void accessVariables(int newValue) {
        this.instanceVariable = newValue; // Accessing instance variable using 'this'
        MyClass.staticVariable = newValue; // Accessing static variable using class name
    }

    // Method to display the current values of instance and static variables
    public void displayValues() {
        System.out.println("Instance variable value: " + this.instanceVariable);
        System.out.println("Static variable value: " + MyClass.staticVariable);
    }

    // Main method to demonstrate usage
    public static void main(String[] args) {
        MyClass obj1 = new MyClass(5);
        MyClass obj2 = new MyClass(10);

        obj1.accessVariables(20);
        obj2.accessVariables(30);

        obj1.displayValues();
        obj2.displayValues();
    }
}
Java

Output

Instance variable value: 20
Static variable value: 20
Instance variable value: 30
Static variable value: 30
Java

• It can be passed as an argument in the constructor call.

Example

public class MyClass {
    private int instanceVariable;
    private static int staticVariable;

    // Constructor initializing instance variables using 'this'
    public MyClass(int instanceVariable) {
        this.instanceVariable = instanceVariable; // 'this' refers to the instance variable 'instanceVariable'
    }

    // Method accessing both instance and static variables
    public void accessVariables(int newValue) {
        this.instanceVariable = newValue; // Accessing instance variable using 'this'
        MyClass.staticVariable = newValue; // Accessing static variable using class name
    }

    // Method to display the current values of instance and static variables
    public void displayValues() {
        System.out.println("Instance variable value: " + this.instanceVariable);
        System.out.println("Static variable value: " + MyClass.staticVariable);
    }

    // Main method to demonstrate usage
    public static void main(String[] args) {
        MyClass obj1 = new MyClass(5);
        MyClass obj2 = new MyClass(obj1.instanceVariable); // Passing 'this' as an argument

        obj1.accessVariables(20);
        obj2.accessVariables(30);

        obj1.displayValues();
        obj2.displayValues();
    }
}
Java

Output

Instance variable value: 20
Static variable value: 20
Instance variable value: 30
Static variable value: 30
Java

• It is used to initiate a constructor of the current class.

Example

public class MyClass {
    private int x;
    private int y;

    // Constructor with two parameters
    public MyClass(int x, int y) {
        this.x = x; // 'this' refers to the instance variable 'x'
        this.y = y; // 'this' refers to the instance variable 'y'
    }

    // Constructor with one parameter, using 'this' to call the other constructor
    public MyClass(int value) {
        this(value, value); // Calls the other constructor with the same value for both 'x' and 'y'
    }

    // Method to display the values of 'x' and 'y'
    public void displayValues() {
        System.out.println("x = " + this.x + ", y = " + this.y);
    }

    // Main method to demonstrate usage
    public static void main(String[] args) {
        MyClass obj1 = new MyClass(5, 10);
        MyClass obj2 = new MyClass(20); // This will call the constructor with one parameter

        obj1.displayValues();
        obj2.displayValues();
    }
}
Java

Output

x = 5, y = 10
x = 20, y = 20
Java

• It can be used to invoke the current class methods.

Example

public class MyClass {
    private int x;

    // Constructor initializing instance variable
    public MyClass(int x) {
        this.x = x; // 'this' refers to the instance variable 'x'
    }

    // Method to display the value of 'x'
    public void displayValue() {
        System.out.println("Value of x: " + this.x);
    }

    // Method to increment the value of 'x' using 'this'
    public void increment() {
        this.x++; // Increment the value of 'x' using 'this'
    }

    // Method to decrement the value of 'x' using 'this'
    public void decrement() {
        this.x--; // Decrement the value of 'x' using 'this'
    }

    // Main method to demonstrate usage
    public static void main(String[] args) {
        MyClass obj = new MyClass(5);
        
        obj.displayValue(); // Display the initial value of 'x'
        
        // Invoke methods using 'this'
        obj.increment();
        obj.displayValue(); // Display the incremented value of 'x'
        
        obj.decrement();
        obj.displayValue(); // Display the decremented value of 'x'
    }
}
Java

Output

Value of x: 5
Value of x: 6
Value of x: 5
Java

Conclusion

In Java, the this keyword refers to the current object instance within a class. It can be used for various purposes:

  1. Distinguishing between instance variables and method parameters with the same name.
  2. Invoking constructors from other constructors within the same class.
  3. Passing the current object as an argument in method calls.
  4. Returning the current object from chaining methods.
  5. Accessing instance and static variables of the current class.
  6. Invoking methods of the current class.

Overall, this enhances code clarity and flexibility by providing a clear reference to the current object in Java programs.

Frequently Asked Questions

What is the Java this keyword?

The this keyword in Java is a reference to the current object within a class.

When should I use the this keyword in Java?

You can use this to distinguish between instance variables and method parameters with the same name, to invoke constructors from other constructors, to pass the current object as an argument in method calls, to return the current object from methods, to access instance and static variables of the current class, and to invoke methods of the current class.

Can this be used inside static methods in Java?

No, this cannot be used inside static methods because static methods are not associated with any specific instance of a class.

Can this be used to refer to static members in Java?

No, this can only be used to refer to instance members (variables and methods) of the current object. It cannot be used to refer to static members because static members belong to the class itself, not to individual instances.

What happens if I use this in a constructor call from another constructor in Java?

When you use this in a constructor call from another constructor, it invokes another constructor of the same class. This is known as constructor chaining.