This Keyword in Java

This Keyword in Java

10 Sep 2024
Beginner
62 Views
40 min read
Learn via Video Course & by Doing Hands-on Labs

Java Online Course Free with Certificate (2024)

Understanding the 'this' Keyword in Java

Understanding "this" keyword in Java is vital for mastering object-oriented programming. It refers to the current object within a class, enabling you to distinguish between instance variables and method parameters with the same name. This improves code clarity and reduces uncertainty, making your code more maintainable and understandable.

In this Java tutorial, we'll look at what "this" keyword is, when to use it, and how it improves object management in Java. So, let us begin with the question, "What is "this" keyword in Java?"

Build your dream career as a full stack developer—sign up for our Java Full Stack Developer Course Training today!

What is this keyword in Java?

What is this keyword in Java?

  • In Java, "this" keyword works as a pointer, informing the program that "I'm talking about this particular object right here."
  • When inside a class, you need to refer to the object the class is currently working on and use "this."
  • It's useful to explain how you're dealing with the object's variables or methods, especially if the variables share the same name.
  • For example, imagine you're filling out a form that asks for "your name." When you write your name, you are referring to yourself, not to someone else. In Java, this does the same thing. It ensures that you are referring to the current object in your code rather than any other object.

Importance of the 'this' Keyword in Java

  • "this" keyword is important because it distinguishes between instance variables (variables that belong to an object) and local variables (variables defined within methods or constructors).
  • It's particularly helpful when the instance variable names match the argument names supplied to a method or constructor.

When and Why should you use 'this'?

  • It would help if you used this when you need to differentiate between instance variables and parameters or when you're chaining constructors or methods.
  • It's also useful for sending the current object as a parameter to another method or constructor.
  • "Call other constructors within the same class."
  • "Retrieve the current object from a method."

"this" Keyword best practices

Now, we will explore the different ways to use"this" keywordin Java.

Referencing Instance Variables

  • Sometimes, the names of method or constructor parameters can be the same as the instance variables of a class.
  • This can create confusion about which variable is being referred to.
  • "this" keyword helps by clearly indicating that you are talking about the instance variable.

Here’s a simple example to show how this is used:

Example

class ScholarHat {
    String courseName;

    ScholarHat(String courseName) {
        this.courseName = courseName; // 'this.courseName' refers to the instance variable
    }

    void displayCourse() {
        System.out.println("Course: " + this.courseName);
    }

    public static void main(String[] args) {
        ScholarHat course = new ScholarHat("Java Full Stack Developer");
        course.displayCourse();
    }
}

Output

Course: Java Full Stack Developer

Explanation

  • In this example, the ScholarHat class has a constructor that initializes the instance variable courseName using the "this" keyword.
  • The displayCourse() method prints the course name to the console.
  • In the main method, an object of ScholarHat is created with the course name "Java Full Stack Developer," and the course name is displayed.

Let’s look at a real-life example involving an Employee class:

Real-Life Example

class Employee {
    String employeeName;
    int employeeID;

    Employee(String employeeName, int employeeID) {
        this.employeeName = employeeName; // 'this.employeeName' refers to the instance variable
        this.employeeID = employeeID; // 'this.employeeID' refers to the instance variable
    }

    void displayEmployeeInfo() {
        System.out.println("Employee Name: " + this.employeeName);
        System.out.println("Employee ID: " + this.employeeID);
    }

    public static void main(String[] args) {
        Employee employee = new Employee("John Doe", 12345);
        employee.displayEmployeeInfo();
    }
}

Output

Employee Name: John Doe
Employee ID: 12345   

Explanation

  • This code defines an Employee Class with two instance variables: employeeName and employeeID.
  • The constructor initializes these variables using the "this" keyword, which differentiates the instance variables from the constructor parameters.
  • The displayEmployeeInfo method prints the employee's name and ID. In the main method, an EmployeeAn object is created with the name "John Doe" and ID, and the displayEmployeeInfo method is used to display this information.

Calling Other Constructors

we can use this to call another constructor in the same class. This is called constructor chaining and helps to avoid duplicate code.

Constructor Chaining with 'this'

  • Constructor chaining with "this" enables a constructor in a class to invoke another constructor in the same class.
  • It helps prevent code duplication by reusing constructor logic, resulting in cleaner and more manageable code.
  • For example, if you have multiple constructors with different parameters, one can call another using this() to initialize the object in a consistent way.

Example

class Vehicle {
    String type;
    String model;

    Vehicle() {
        this("Car", "Generic Model"); // Calls the constructor with two parameters
    }

    Vehicle(String type, String model) {
        this.type = type;
        this.model = model;
    }

    void display() {
        System.out.println("Vehicle Type: " + this.type + ", Model: " + this.model);
    }

    public static void main(String[] args) {
        Vehicle vehicle1 = new Vehicle();
        Vehicle vehicle2 = new Vehicle("Bike", "Mountain Model");
        vehicle1.display();
        vehicle2.display();
    }
}

Output

Vehicle Type: Car, Model: Generic Model
Vehicle Type: Bike, Model: Mountain Model

Explanation

  • The Vehicle class has two constructors. The default constructor calls another constructor using this() with default values for type and model.
  • The parameterized constructor initializes the instance variables type and model with provided values.
  • The display() method prints the type and model of the vehicle to the console.
  • In the main method, two Vehicle objects are created. The first uses the default constructor, and the second uses the parameterized constructor. Both vehicles' details are displayed using the display() method.

    Real-Life Example

    class Pizza {
        String size;
        String crustType;
    
        Pizza() {
            this("Medium", "Thin Crust"); // Calls the constructor with two parameters
        }
    
        Pizza(String size, String crustType) {
            this.size = size;
            this.crustType = crustType;
        }
    
        void display() {
            System.out.println("Pizza Size: " + this.size + ", Crust: " + this.crustType);
        }
    
        public static void main(String[] args) {
            Pizza pizza1 = new Pizza();
            Pizza pizza2 = new Pizza("Large", "Stuffed Crust");
            pizza1.display();
            pizza2.display();
        }
    }
    

    Output

    Pizza Size: Medium, Crust: Thin Crust
    Pizza Size: Large, Crust: Stuffed Crust
    

    Explanation

    • The Pizza class has two constructors. The default constructor calls another constructor using this() with default values for size and crustType.
    • The parameterized constructor initializes the instance variables size and crustType with the values provided as arguments.
    • The display() method prints the size and crust type of the pizza to the console.
    • In the main method, two Pizza objects are created. The first uses the default constructor, and the second uses the parameterized constructor. Both pizzas' details are displayed using the display() method.

    Passing 'this' as an Argument

    Passing this as an argument lets you use the current object inside methods or constructors.

    Using 'this' in Method Calls

    • We can pass the current object as an argument to other methods using "this" keyword.
    • This is useful when one object needs to pass itself to another object’s method for processing.

    Using 'this' in Constructor Calls

    • Similarly, we can pass this in constructor calls to pass the current object as an argument within the same class.
    Let's understand this concept with an example.

      Example

      class Printer {
          void print(Employee emp) {
              System.out.println("Employee Name: " + emp.name);
          }
      }
      
      class Employee {
          String name;
      
          Employee(String name) {
              this.name = name;
          }
      
          void printEmployee() {
              Printer printer = new Printer();
              printer.print(this); // Passing the current object as an argument
          }
      
          public static void main(String[] args) {
              Employee emp = new Employee("Alice");
              emp.printEmployee();
          }
      }
      

      Output

      Employee Name: Alice
      

      Explanation

      • The PrintBalancethe class will have a method called print(), which takes a BankAccount argument and prints the account's balance.
      • The BankAccount class contains a balance field and a constructor to initialize this field.
      • The BankAccountthe class also includes a printBalance() method that creates an instance of the Printer class and calls its print() method, passing this (the current BankAccount object) as an argument.
      • In the main method, a BankAccountthe object is created with a balance of $5000.0. Then, the printBalance() method is called on this object, printing the account balance using the Printer object.

      Real-Life Example

      class Printer {
          void print(BankAccount account) {
              System.out.println("Account Balance: $" + account.balance);
          }
      }
      
      class BankAccount {
          double balance;
      
          BankAccount(double balance) {
              this.balance = balance;
          }
      
          void printBalance() {
              Printer printer = new Printer();
              printer.print(this); // Passing the current object as an argument
          }
      
          public static void main(String[] args) {
              BankAccount account = new BankAccount(5000.0);
              account.printBalance();
          }
      }
      

      Output

      Account Balance: $5000.0
      

      Explanation

      • The Printer class has a print()a method that takes a BankAccount object as an argument and prints the account's balance.
      • The BankAccount class has a balance field and a constructor to initialize it.
      • The printBalance() method in the BankAccount class creates a Printer object and calls its print() method, passing this (the current BankAccount object) as an argument.
      • In the main method, a BankAccount object with a balance of $5000.0 is created. The printBalance() method is then called on this object, which prints the account balance using the Printer object.

      Calling the Current Class Method

      This term allows us to explicitly invoke another method within the same class. This is especially handy when you wish to emphasize that the method being called is part of the same object.

        Example

        class MyClass {
            void method1() {
                System.out.println("Method 1");
                this.method2(); // Explicitly calling method2
            }
        
            void method2() {
                System.out.println("Method 2");
            }
        
            public static void main(String[] args) {
                MyClass obj = new MyClass();
                obj.method1();
            }
        }
        

        Output

        Method 1
        Method 2
        

        Explanation

        • The MyClass class has two methods: method1() and method2().
        • The method1() method prints "Method 1" to the console and then explicitly calls method2() using this.method2().
        • The method2() method prints "Method 2" to the console.
        • In the main method,MyClassthe object is created, and method1() is called on this object. This results in "Method 1" being printed first, followed by "Method 2" due to the call to method2() within method1().

        Real-Life Example

        class Person {
            String name;
            int age;
        
            void setName(String name) {
                this.name = name;
                this.updateProfile(); // Calling updateProfile to reflect changes
            }
        
            void updateProfile() {
                System.out.println("Updated Profile - Name: " + this.name + ", Age: " + this.age);
            }
        
            public static void main(String[] args) {
                Person person = new Person();
                person.setName("Alice");
                person.age = 30;
                person.updateProfile();
            }
        }
        

        Output

        Updated Profile - Name: Alice, Age: 0
        Updated Profile - Name: Alice, Age: 30
        

        Explanation

        • The Personthe class has two fields: name and age.
        • The setName() method updates the name field and then calls the updateProfile() method to print the updated profile information.
        • The updateProfile() method prints the current values of the name and age fields to the console.
        • In the main method, an object is created. The setName() method is called to set the name to "Alice" and update the profile, printing "Alice" with the default age of 0. Then, the agefield is set to 30 and updateProfile() is called again, and the updated profile with age 30 is printed.

        Using 'this' as the Return Type of a Method

        Fluent Interface Pattern

        • The Fluent Interface Pattern is a design approach used to create more readable and intuitive code by allowing method calls to be chained.
        • By returning this from methods, you enable multiple method calls on a single line, making your code cleaner and easier to understand.
        • This pattern is particularly useful in scenarios where objects are configured or modified through a series of method calls.

        Method Chaining with 'this'

        • Method chaining involves calling multiple methods in a single statement.
        • By returning this from each method, you allow the next method to be called on the same object.
        • This approach enhances code readability and conciseness, making it easier to follow the sequence of operations.

        Example

        class ChainingExample {
            int value;
        
            ChainingExample setValue(int value) {
                this.value = value;
                return this; // Returning the current object
            }
        
            ChainingExample incrementValue(int increment) {
                this.value += increment;
                return this; // Returning the current object
            }
        
            void display() {
                System.out.println("Value: " + this.value);
            }
        
            public static void main(String[] args) {
                ChainingExample obj = new ChainingExample();
                obj.setValue(5).incrementValue(10).display(); // Method chaining
            }
        }
        

        Output

        Value: 15
        

        Explanation

        • The ChainingExamplethe class has a field value and three methods: setValue(), incrementValue(), and display().
        • The setValue() method sets the value field and returns the current object (this) to allow method chaining.
        • The incrementValue() method adds the specified increment to the value field and returns the current object (this) to allow method chaining.
        • The display() method prints the current value of the value field to the console.
        • In the main method, an instance of ChainingExample is created. Method chaining is used to set the value to 5, increment it by 10, and then display the final value, which results in 15 being printed.

        Real-Life Example

        class PizzaOrder {
            String size;
            String toppings;
        
            PizzaOrder setSize(String size) {
                this.size = size;
                return this; // Returning the current object
            }
        
            PizzaOrder addToppings(String toppings) {
                this.toppings = toppings;
                return this; // Returning the current object
            }
        
            void finalizeOrder() {
                System.out.println("Pizza Order - Size: " + this.size + ", Toppings: " + this.toppings);
            }
        
            public static void main(String[] args) {
                PizzaOrder order = new PizzaOrder();
                order.setSize("Large").addToppings("Pepperoni").finalizeOrder(); // Method chaining
            }
        }
        

        Output

        Pizza Order - Size: Large, Toppings: Pepperoni
        

        Explanation

        • The PizzaOrderthe class has two fields: size and toppings, and three methods: setSize(), addToppings(), and finalizeOrder().
        • The setSize() method sets the size field and returns the current object (this) to enable method chaining.
        • The addToppings() method sets the toppings field and returns the current object (this) to enable method chaining.
        • The finalizeOrder() method prints the details of the pizza order to the console.
        • In the main method, an instance of PizzaOrder is created. Method chaining is used to set the size to "Large," add "Pepperoni" as toppings, and finalize the order, resulting in the complete order details being printed.

        'this' in Inner Classes

        Using this in inner classes provides a way to reference and utilize the context of the outer class. This is particularly helpful when the inner class needs to work closely with the outer class, ensuring that the two classes are seamlessly integrated.

        Accessing Outer Class Members

        The ability to access the outer class’s members directly from the inner class can simplify your code and make it more coherent. Let's understand with a simple example.

          Example

          class Outer {
              int outerValue = 10;
          
              class Inner {
                  void display() {
                      // Accessing the outer class's member using 'Outer.this'
                      System.out.println("Outer Value: " + Outer.this.outerValue);
                  }
              }
          
              public static void main(String[] args) {
                  Outer outer = new Outer();
                  Outer.Inner inner = outer.new Inner();
                  inner.display();
              }
          }
          

          Output

          Outer Value: 10
          

          Explanation

          • The Outerthe class has a field outerValue initialized to 10.
          • Within the Outer class, there is a non-static inner class Inner.
          • The display() method in the Inner class accesses the outerValue field of the Outer class using Outer.this.outerValue, which allows the inner class to reference the outer class's members.
          • In the main method, an instance of Outer is created. Then, an instance of the inner class Inner is created using the outer object. The display() method is called on the inner class instance, which prints the value of outerValue.

          Real-Life Example

          class House {
              String address = "123 Main St";
              int floors = 2;
          
              class Room {
                  void display() {
                      // Accessing the outer class's members using 'House.this'
                      System.out.println("House Address: " + House.this.address);
                      System.out.println("Number of Floors: " + House.this.floors);
                  }
              }
          
              public static void main(String[] args) {
                  House house = new House();
                  House.Room room = house.new Room();
                  room.display();
              }
          }
          

          Output

          House Address: 123 Main St
          Number of Floors: 2
          

          Explanation

          • The Housethe class has two fields: address, initialized to "123 Main St", and floors, initialized to 2.
          • Within the House class, there is a non-static inner class Room.
          • The display() method in the Room class accesses the address and floors fields of the House class using House.this.address and House.this.floors, respectively.
          • In the main method, an instance of House is created. An instance of the inner class Room is then created using the house object. The display() method is called on the inner class instance, which prints the address and the number of floors of the house.

          Common Mistakes and Pitfalls with 'this'

          "this"keyword in Java is useful for referring to the current object, but it can cause issues if used incorrectly. Common difficulties include hiding instance variables and causing confusion in static methods. Understanding these problems allows you to write more readable and dependable code.

          Shadowing of Instance Variables

          Explanation of Variable Shadowing

          • Shadowing happens when a local variable or parameter has the same name as an instance variable.
          • This means that inside a method, the local variable "shadows" or hides the instance variable with the same name.
          • This can be confusing because you might accidentally change or use the wrong variable.

          How 'this' Helps Avoid Shadowing Issues

          • To avoid confusion, you can use "this" to refer specifically to the instance variable when writing this.
          • Variable name: you make it clear that you are talking about the instance variable, not the local variable. This helps ensure that you are working with the correct variable.

          Example

          class ShadowingExample {
              int value = 10; // This is the instance variable
          
              void setValue(int value) {
                  // The parameter 'value' shadows the instance variable 'value'
                  this.value = value; // Use 'this' to refer to the instance variable
              }
          
              void display() {
                  System.out.println("Instance Value: " + this.value);
              }
          
              public static void main(String[] args) {
                  ShadowingExample example = new ShadowingExample();
                  example.setValue(20); // Set the instance variable to 20
                  example.display();   // Display the instance variable
              }
          }
          

          Output

          Instance Value: 20
          

          Explanation

          • The ShadowingExamplethe class has an instance variable value initialized to 10.
          • The setValue()the method has a parameter named value, which shadows the instance variable. With this method, this.value is used to refer to the instance variable while value refers to the method parameter.
          • The display() method prints the current value of the instance variable value.
          • In the main method, an instanceShadowingExample is created. The setValue() method is called with the argument 20, which sets the instance variable value to 20. The display() method is then called to print the updated value of the instance variable, resulting in "Instance Value: 20" being printed.

          Misunderstanding 'this' in Static Context

          • In Java, "this" refers to the current instance of a class. However, this can be perplexing when working with static methods.
          • Static methods are associated with the class rather than any specific instance of the class.
          • As a result, static methods cannot access this.
          • In simple terms, because static methods are not bound to any specific object, they cannot be used to reference instance variables or methods.

          Why 'this' Cannot Be Used in Static Methods

          • The "this" keyword in Java relates to the current object.
          • Static methods, on the other hand, are unique in that they are part of the class and not associated with a specific object.
          • They cannot use static methods because they do not operate with specific objects.
          • If you try to utilize "this" in a static method, it will throw an error because there is no instance context to refer to.

          Example

          class StaticExample {
              int instanceValue = 10; // This is an instance variable
          
              static void staticMethod() {
                  // Trying to use 'this' in a static method will cause an error
                  // System.out.println(this.instanceValue); // This line will cause an error
              }
          
              void instanceMethod() {
                  // Using 'this' in an instance method is correct
                  System.out.println("Instance Value: " + this.instanceValue);
              }
          
              public static void main(String[] args) {
                  StaticExample example = new StaticExample();
                  example.instanceMethod(); // This works fine
                  StaticExample.staticMethod(); // This works, but 'this' cannot be used here
              }
          }
          

          Output

          Instance Value: 10
          

          Explanation

          • The StaticExamplethe class has an instance variable instanceValue initialized to 10.
          • The staticMethod() is a static method. Static methods cannot use the "this" keywordbecause this refers to the current instance, and static methods do not belong to any particular instance.
          • The instanceMethod() is an instance method. It can be usedthis to refer to the instance variable.instanceValue.
          • In the main method, an instanceStaticExample is created. This instanceMethod() is called, in this instance, printing the value of instanceValue. The staticMethod() is called using the class name, but it does not interact with this and is commented out to avoid compilation errors.
          Summary

          "this" keyword in Java is essential for distinguishing between instance variables and method arguments, especially when they have the same name. It improves code clarity, enables method chaining, and helps to avoid common errors such as variable shadowing. Understanding and exploiting this allows developers to produce better maintainable and readable code.Upgrade your Java skills with ScholarHat's Full-Stack Java Developer Certification Training. Enroll now to learn key concepts like these and more!

            FAQs

            Q1. How many ways to use this keyword in Java?

            "this" keyword in Java can be used in a variety of ways, including referring to the current object's instance variables, invoking the current class's methods, calling one constructor from another within the same class, and passing the current object as a parameter to a method or constructor.

            Q2. What is this keyword in Java interview questions?

            In Java interviews, queries regarding this keyword frequently revolve around its role in distinguishing between instance variables and arguments, its use in method and constructor calls, and its ability to send the current object as an argument. These questions assess the candidate's knowledge of object-oriented principles and code clarity.

            Q3. Can this keyword pass itself to another method?

            Yes, you can use the "this" keyword to send the current object instance to another method. This gives the method access to the object's attributes and methods, allowing for interactions within or between classes.

            Q4. Is this keyword necessary?

            "this" keyword is not usually required, but it is required when you need to explicitly reference the current object, such as to distinguish between instance variables and method parameters with the same name or to enable method chaining.
            Share Article

            Live Classes Schedule

            Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
            ASP.NET Core Certification TrainingSep 21SAT, SUN
            Filling Fast
            09:30AM to 11:30AM (IST)
            Get Details
            Advanced Full-Stack .NET Developer Certification TrainingSep 21SAT, SUN
            Filling Fast
            09:30AM to 11:30AM (IST)
            Get Details
            Software Architecture and Design TrainingSep 22SAT, SUN
            Filling Fast
            07:00AM to 09:00AM (IST)
            Get Details
            .NET Solution Architect Certification TrainingSep 22SAT, SUN
            Filling Fast
            07:00AM to 09:00AM (IST)
            Get Details
            ASP.NET Core Certification TrainingSep 29SAT, SUN
            Filling Fast
            08:30PM to 10:30PM (IST)
            Get Details
            Advanced Full-Stack .NET Developer Certification TrainingSep 29SAT, SUN
            Filling Fast
            08:30PM to 10:30PM (IST)
            Get Details
            Full-Stack .NET Developer Certification TrainingOct 06SAT, SUN
            Filling Fast
            07:00AM to 09:00AM (IST)
            Get Details
            Angular Certification TrainingOct 06SAT, SUN
            Filling Fast
            08:30PM to 10:30PM (IST)
            Get Details
            .NET Solution Architect Certification TrainingOct 13SAT, SUN
            Filling Fast
            05:30PM to 07:30PM (IST)
            Get Details
            .NET Microservices Certification TrainingOct 13SAT, SUN
            Filling Fast
            05:30PM to 07:30PM (IST)
            Get Details
            ASP.NET Core ProjectOct 13SAT, SUN
            Filling Fast
            10:00AM to 12:00PM (IST)
            Get Details
            Advanced Full-Stack .NET Developer Certification TrainingOct 20SAT, SUN
            Filling Fast
            09:30AM to 11:30AM (IST)
            Get Details
            ASP.NET Core Certification TrainingOct 20SAT, SUN
            Filling Fast
            09:30AM to 11:30AM (IST)
            Get Details
            Azure Developer Certification TrainingOct 27SAT, SUN
            Filling Fast
            08:30PM to 10:30PM (IST)
            Get Details

            Can't find convenient schedule? Let us know

            About Author
            Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

            Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
            Accept cookies & close this