This keyword in java

This keyword is used to refer current object

- It can be used to refer current class instance variable.

- It can be used to invoke the current class method (implicitly)

- It can be used to invoke the current class constructor.

- It can be passed as an argument in the method call.

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

- It can be used to return the current class instance from the method

 

Use case demonstrating this as current class instance variables

 
//refer to current class instance variables

public class Test {
    int x;
    int y;
    // Parameterized constructor
    Test(int x, int y) {
        this.x = x;
        this.y = x;
    }
    void display() {
        //Displaying value of variables x and y
        System.out.println("x = " + x + "  y = " + y);
    }
    public static void main(String[] args) {
        Test t = new Test(10, 20);
        t.display();
    }
}

 

Output:

x = 10  y = 10

 

Here in this sample when the  test object t is instantiated the Parameterized constructor is executed thus the value of x and y is assigned to 10 1nd 20 respectively next statement executes the display method which will print the value of x and y

 

Use a case demonstrating this to invoke the current class constructor

 
public class MySample {
    int x;
    int y;
    //Default constructor
    MySample() {
        this(10, 20);
        System.out.println("Inside  default constructor \n");
    }
    //Parameterized constructor
    MySample(int x, int y) {
        this.x = x;
        this.y = y;
        System.out.println("Inside parameterized constructor");
    }
    public static void main(String[] args) {
        MySample m = new MySample();
    }
}

 

Output:

Inside parameterized constructor
Inside  default constructor

 

In this example, we have two constructors, a default constructor, and a parameterized constructor. When we do not pass any parameter while creating the object, using a new keyword default constructor is invoked which has the statement this(10,20) will call the parameterized constructor and print the statement” Inside parameterized constructor”. After that statement inside the default constructor is printed, however when you pass a parameter the parameterized constructor that matches with the passed parameters list gets invoked.

 

Use a case demonstrating this to return the current class instance

 
public class Sample {
    int x;
    int y;
    //Default constructor
    Sample() {
        x = 10;
        y = 20;
    }
    //Method that returns a current class instance
    Sample get() {
        return this;
    }
    //Displaying value of variables a and b
    void display() {
        System.out.println("x = " + x + "  y = " + y);
    }
    public static void main(String[] args) {
        Sample s = new Sample();
        s.get().display();
    }
}

 

Output:

x = 10  y = 20
 

In this example when the object s is instantiated the default constructor gets invoked which assigns the value of x and y later when the next statement gets executed get method returns the value of the current instance which is displayed through the statements in the display method

 

 

Related Tutorials

Map