this keyword in Java

In this article we will see this keyword in Java.

this keyword in Java

this keyword in Java is a special keyword which can be used to represent current object or instance of any class in Java. “this” keyword can also call constructor of same class in Java and used to call overloaded constructor. In this article we will see how to use this keyword in Java and different examples. this sometime also associate with super keyword which is used to denote instance of super class in Java and can be used to call overloaded constructor in Java.

6 usage of this keyword

1. to refer current class instance variable

Program


//Java code for using 'this' keyword to
//refer current class instance variables
class demo
{
    int a;
    int b;
     
    // parameterized constructor
    demo(int a, int b)
    {
        this.a = a;
        this.b = b;
    }
 
    void display()
    {
        //displays the value of variables a and b
        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
}

public class Main
    {
        public static void main(String[] args)
        {
            demo object = new demo(10, 20);
            object.display();
        }
}
  
  

Output


a = 10
b = 20
  

Explanation

The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity. In the above example, parameters (formal arguments) and instance variables are same. So, we are using this keyword to distinguish local variable and instance variable.If local variables(formal arguments) and instance variables are different, there is no need to use this keyword like in the following program.It is better approach to use meaningful names for variables. So we use same name for instance variables and parameters in real time, and always use this keyword.

2. to invoke current class method

Program


// this keyword in class method
class demo 
{
    void display() // 3
    {
        // calling fuction show()
        this.show(); // 4
       System.out.println("We are inside display function"); // 7
    }
     
    void show()  // 5
    { 
        System.out.println("We are inside show funcion"); // 6
    }
}

public class Main
{
    public static void main(String args[]) 
    {
        demo t1 = new demo();  // 1
        t1.display(); // 2
    }
}
  
  

Output


We are inside show funcion
We are inside display function
  

3. to invoke current class constructor

Program


class demo
{
    int a;
    int b;
 
    //2. Default constructor
    demo()
    {  
        //3. calling parameterized constructor of this class
        this(10, 20);
        //5. back in default constructor
        System.out.println("We are inside  default constructor \n");
    }
     
    //4. Parameterized constructor
    demo(int a, int b)
    {
        this.a = a;
        this.b = b;
        System.out.println("We are inside parameterized constructor");
    }
}

public class Main
{
    public static void main(String[] args)
    {
        //1. calls default constructor
        demo object = new demo();
    }
}
  
  

Output


We are inside parameterized constructor
We are inside  default constructor
  

4. as an argument in the method call

Program


// this keyword in method parameter
class demo
{
    int a;
    int b;
     
    // Default constructor
    demo()
    {
        a = 10;
        b = 20;
    }
     
    // Method that receives 'this' keyword as parameter
    void display(demo obj) // 5
    {
        System.out.println("a = " + a + "  b = " + b); // 6
    }
  
    // Method that returns current class instance
    void get() // 3
    {
        display(this); // 4
    }
}

public class Main
{ 
    public static void main(String[] args)
    {
        demo object = new demo(); // 1
        object.get(); // 2
    }
}
  
  

Output


a = 10 b = 20
  

5. as argument in the constructor call

Program


class A
{
    B obj;
     
    // Parameterized constructor with object of B as a parameter
    A(B obj) // 4
    {
        this.obj = obj; // 5
        
     // calling display method of class B
        obj.display(); // 6
    }
     
}
 
class B
{
    int x = 5;
     
    // Default Contructor that create a object of A  with passing this as an argument in the constructor
    B() // 2
    {
        A obj = new A(this); // 3
    }
     
    // method to show value of x 
    void display()  // 7
    {
        System.out.println("Value of x in Class B is " + x); // 8
    }
     
    public static void main(String[] args) {
        B obj = new B();
    }
}

public class Main
{
    public static void main(String[] args) 
    {
        B obj = new B(); // 1
    }
}
  
  

Output


Value of x in Class B is 5
  

6. to return the current class instance from the method

Program


//to return the current class instance
class demo
{
    int a;
    int b;
 
    //Default constructor
    demo()
    {
        a = 10;
        b = 20;
    }
     
    //Method that returns current class instance
    // 3
    demo get()
    {
        // 4. returns the instance of class demo
        return this;
    }
     
    //Displaying value of variables a and b
    void display() // 6
    {
        System.out.println("a = " + a + "  b = " + b);  // 7
    }
}

public class Main
{
    public static void main(String[] args)
    {
        //1. create object of class demo
        demo object = new demo();
        object.get().display(); // 2 // 5
    }
}
  
  

Output


a = 10 b = 20
  

Comments

Popular posts from this blog

System.Environment

System.Console

Datatype and keyword