Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Java For Automation

  • What is Java?
  • Why to Learn Java?
  • History of Java
  • Types of Java Applications
  • Naming Conventions
  • Hello World Program
  • Internal Details of Java program
  • What is Java JDK, JRE and JVM ?
  • What is Java Comments ?
  • What is Java Variable?
  • Variable Naming convention
  • Primitive Data Types
  • Non Primitive Data Types
  • Type Casting
  • Java Arithmetic Operators
  • Relational Operators
  • What is logical operators in Java? 
  • What is Assignment Operators in Java? 
  • What is Unary Operators in Java? 
  • What is Ternary Operators in Java?
  • What is Java Control Statements?
  • What are Decision Making statements?
  • What are looping statements?
  • What are Jump statements?
  • What is OOPS concept in Java?
  • Advantages of Java OOPs
  • What is Java Inheritance?
  • What is Java Constructor?
  • What is this keyword?
  • What is Polymorphism?
  • What is super keyword ?
  • What is final keyword? 
  • What are access modifiers?
  • What is Encapsulation? 
  • What is Abstraction? 
View Categories
  • Home
  • Tutorials
  • Java
  • Java For Automation
  • What is this keyword?

What is this keyword?

What is this keyword?

The this keyword in Java is a reference to the current object instance of a class. It is used to differentiate between instance variables and local variables or parameters that have the same name, and to pass the current object as a parameter to other methods.

Key Uses of this Keyword: #

Distinguishing Between Instance Variables and Parameters: When method or constructor parameters have the same name as instance variables, the this keyword helps to differentiate between them.

Invoking Current Object’s Methods: The this keyword can be used to call other methods of the same class from within a method.

Passing the Current Object as a Parameter: The this keyword can be used to pass the current object to another method or constructor.

Returning the Current Object: The this keyword can be used to return the current object from a method.

Examples: #

1. Distinguishing Between Instance Variables and Parameters:

class Person {

    String name;

    int age;

    // Constructor with parameters

    Person(String name, int age) {

        // 'this.name' refers to the instance variable

        // 'name' refers to the parameter

        this.name = name;

        this.age = age;

    }

    void display() {

        System.out.println("Name: " + this.name);

        System.out.println("Age: " + this.age);

    }

}

public class Main {

    public static void main(String[] args) {

        Person p = new Person("Alice", 30);

        p.display();

    }

}

In this example, this.name and this.age refer to the instance variables, while name and age are the constructor parameters. The this keyword helps to clarify that we are referring to the instance variables.

2. Invoking Current Object’s Methods:

class Calculator {

    void add(int a, int b) {

        int result = a + b;

        System.out.println("Sum: " + result);

    }

    void performAddition() {

        // Calling the add method of the same class

        this.add(5, 10);

    }

}

public class Main {

    public static void main(String[] args) {

        Calculator calc = new Calculator();

        calc.performAddition();

    }

}

In this example, this.add(5, 10) is used to call the add method from within another method of the same class.

3. Passing the Current Object as a Parameter:

class Printer {

    void printDetails(Person p) {

        System.out.println("Printing details of: " + p.name);

    }

    void print() {

        // Passing the current object as a parameter

        printDetails(this);

    }

}

class Person {

    String name;

    Person(String name) {

        this.name = name;

    }

}

public class Main {

    public static void main(String[] args) {

        Person p = new Person("Bob");

        Printer printer = new Printer();

        printer.print();

    }

}

In this example, this is used to pass the current Printer object to the printDetails method.

4. Returning the Current Object:

class Chain {

    int value;

    Chain(int value) {

        this.value = value;

    }

    Chain setValue(int value) {

        this.value = value;

        return this; // Returning the current object

    }

    void display() {

        System.out.println("Value: " + this.value);

    }

}

public class Main {

    public static void main(String[] args) {

        Chain obj = new Chain(10);

        obj.setValue(20).display(); // Chaining method calls

    }

}

In this example, the setValue method returns the current object (this), allowing method chaining.

Summary #

The this keyword is a powerful feature in Java that helps to refer to the current instance of a class. It resolves naming conflicts, allows method chaining, and provides the ability to pass the current object as a parameter or return it from a method.

Java
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
What is Java Constructor?What is Polymorphism?
Table of Contents
  • Key Uses of this Keyword:
  • Examples:
  • Summary
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105