This keyword in Java | Method, Reference, constructor, a​nd examples

What is the “This” Keyword?

Java “this” Keyword works as a reference to the current Object in the program. It can be used inside the Method or constructor of Java Class.

This keyword in Java

Where use This keyword in Java?

Here is a list of this keyword can be used to:-

  • Refer instance variable of the current class.
  • Invoke or initiate current class constructor.
  • Passed as an argument (Parameters) in the method call.
  • Passed as argument (Parameters) in the constructor call.
  • Return the current class instance.

Java this keyword program example code

Let’s see the simple this keyword is used in java programs or how to use it.

Instance Variable | Field

In this example using ‘this’ keyword to refer to current class instance variables.

See below code how to using this.num2 and this.num2 in the constructor.

class TestCode {
    int num1;
    int num2;

    // Parameterized constructor 
    TestCode(int num1, int num2) {
        this.num1 = num1;
        this.num2 = num2;
    }

    // method
    void show() {
        //Displaying value of variables a and b 
        System.out.println("Number 1 = " + num1 + "  Number 2 = " + num2);
    }

    public static void main(String[] args) {
        // creating class object
        TestCode testCode = new TestCode(12, 10);
        testCode.show();
    }
} 

Output:

Number 1 = 12 Number 2 = 10

“this” keyword in Java method

Many teams use this keyword with java methods. Let’s see one by one with examples.

1. Use ‘this’ keyword as method parameter

class TestCode {

    int num1;
    int num2;

    // Default constructor
    TestCode() {
        num1 = 10;
        num1 = 20;
    }

    // Method with 'this' keyword as parameter
    void display(TestCode object) {
        System.out.println("Number 1 = " + num1 + "  Number 2 = " + num2);
    }

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

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

Output:

Number 1 = 20 Number 2 = 0

2. Use ‘this’ keyword to invoke current class method

this keyword can be used inside Methods to call another Method in the same Class.

class TestCode {

    void msg() {
        // call show() method
        this.show();

        System.out.println("Message Method");
    }

    void show() {
        System.out.println("Show Method");
    }


    public static void main(String args[]) {
        TestCode testCode = new TestCode();
        testCode.msg();
    }
} 

Output:

Show Method
Message Method

In a constructor used “this” Keyword

How to invoke the current class constructor?

Using this keyword word to invoke the current class constructor. See below the example program of Java “this” in the constructor.

class TestCode {
    int num1;
    int num2;

    //Default constructor
    TestCode()
    {
        this(10, 20);
        System.out.println("Default constructor");
    }

    //Parameterized constructor
    TestCode(int a, int b)
    {
        this.num1 = a;
        this.num2 = b;
        System.out.println("Parameterized constructor");
    }

    public static void main(String[] args)
    {
        // creating object of class
        TestCode testCode = new TestCode();
    }
} 

Output:

Parameterized constructor
Default constructor

Get The class instance of Using ‘this’ keyword

Let’s see the example of get the current instance of a class using a method.

class TestCode {

    int num1;
    int num2;

    // Default constructor
    TestCode() {
        num1 = 20;
        num1 = 40;
    }

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

    //Displaying value of variables
    void display()
    {
        System.out.println("Number 1 = " + num1 + "  Number 2 = " + num2);
    }

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

Do comment if you have any doubts and suggestions on this tutorial.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Java this keyword examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.