๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

"this" Keyword in Java  


Introduction
  • this keyword is a reference variable in Java.
  • It refers to the current class object (the object whose method or constructor is being invoked).
  • this keyword in Java
  • Use of this Keyword:
    1. It is used to refer to the current class instance variable.
    2. It is used to refer to the current class method.
    3. It is used to refer to the current class constructor.
    4. It is used to pass the current class instance as a parameter to the method.
    5. It is used to pass the current class instance as a parameter to the constructor.
    6. It is used to return the current class instance from the method.

1. It is used to refer to the current class instance variable.
  • Java Program Example 1:
    public class ThisDemo
    {
        int no = 10;
    
        void m1(int no)
        {
            System.out.println("1. no : "+no);     // Prints the value of the local parameter 'no' (method argument)
    
            System.out.println("2. no : "+this.no);    //'this.no' refers to the instance variable of the current object
        }
    
        public static void main(String[] args)
        {
            ThisDemo obj = new ThisDemo();
            obj.m1(20);
        }
    }
  • Java Program Example 2:
    class Student
    {
        int id;
        String name;
    
        // Constructor with parameters having the same name as instance variables
        Student(int id, String name)
        {
            this.id = id;         // 'this.id' refers to the instance variable
            this.name = name;     // 'name' on the right refers to the parameter
        }
    
        void display()
        {
            System.out.println("ID: " + id);
            System.out.println("Name: " + name);
        }
    
        public static void main(String[] args)
        {
            Student s1 = new Student(101, "Amit");
            s1.display();
        }
    }

2. It is used to refer to the current class method.
  • Java Program Example:
    public class ThisDemo
    {
        void showMessage()
        {
            System.out.println("Hello from showMessage() method");
        }
    
        void display()
        {
            // Calling another method of the same class using 'this'
            this.showMessage();  // Optional: can also call showMessage() directly
            System.out.println("Inside display() method");
        }
    
        public static void main(String[] args)
        {
            ThisDemo obj = new ThisDemo();
            obj.display();
        }
    }

3. It is used to refer to the current class constructor.
  • Java Program Example:
    public class ThisDemo
    {
        // Default constructor
        ThisDemo()
        {
            System.out.println("Default constructor called");
        }
    
        // Parameterized constructor
        ThisDemo(String name)
        {
            // Calling default constructor using 'this()'
            this();  // Must be the first statement in the constructor
            System.out.println("Hello, " + name);
        }
    
        public static void main(String[] args)
        {
            // Creating object using parameterized constructor
            ThisDemo obj = new ThisDemo("Deepak");
        }
    }
    • Note: this() must be the first statement in a constructor
      • If used, the call to another constructor in the same class (this()) must appear as the very first statement in the constructor.
      • It is used to achieve constructor chaining within the same class.

4. It is used to pass the current class instance as a parameter to the method.
  • Java Program Example:
    public class ThisDemo
    {
        void display(ThisDemo obj)
        {
            System.out.println("display() method is called");
        }
    
        void call()
        {
            // Passing current object as argument using 'this'
            display(this);
        }
    
        public static void main(String[] args)
        {
            ThisDemo obj = new ThisDemo();
            obj.call();
        }
    }

5. It is used to pass the current class instance as a parameter to the constructor.
  • Java Program Example:
    class A
    {
        int value = 10;
    
        A()
        {
            // Passing current instance of A to B's constructor
            B b = new B(this);
        }
    }
    
    class B
    {
        // Constructor of class B that accepts a reference to class A
        B(A obj)
        {
            System.out.println("Constructor of B is called");
            System.out.println("Value from class A: " + obj.value);
        }
    }
    
    public class ThisDemo
    {
        public static void main(String[] args)
        {
            A a = new A();
        }
    }

6. It is used to return the current class instance from the method.
  • Java Program Example:
    public class ThisDemo
    {
        int no;
    
        // Method that sets the no and returns the current class instance
        ThisDemo setValue(int no)
        {
            this.no = no;
            return this; // returning current object
        }
    
        // Method to display the no
        void display()
        {
            System.out.println("Number: " + no);
        }
    
        public static void main(String[] args)
        {
            // Method chaining using returned instance
            ThisDemo obj = new ThisDemo();
            obj.setValue(100).display();  // chaining method call
        }
    }