Java this Keyword

🔹 Java this Keyword

The this keyword in Java is a reference variable that refers to the current object. It is mainly used to differentiate instance variables from local variables and to call constructors or methods of the current class.


✅ 1. Uses of this Keyword

  1. Referring to Current Object

  2. Accessing Instance Variables (when shadowed by local variables)

  3. Calling Another Constructor (constructor chaining)

  4. Passing Current Object as a Parameter

  5. Calling Current Class Methods


✅ 2. Example 1: Accessing Instance Variables

class Person {
String name;

void setName(String name) {
this.name = name; // 'this.name' refers to instance variable
}

void display() {
System.out.println("Name: " + name);
}
}

public class Main {
public static void main(String[] args) {
Person p = new Person();
p.setName("Alice");
p.display();
}
}

📌 Output:

Name: Alice

Without this, name = name; would refer to the local parameter, not the instance variable.


✅ 3. Example 2: Calling Another Constructor (Constructor Chaining)

class Car {
String brand;
int year;

Car() {
this("Unknown", 0); // call parameterized constructor
}

Car(String brand, int year) {
this.brand = brand;
this.year = year;
}

void display() {
System.out.println("Brand: " + brand + ", Year: " + year);
}
}

public class Main {
public static void main(String[] args) {
Car car1 = new Car();
Car car2 = new Car("Toyota", 2022);

car1.display();
car2.display();
}
}

📌 Output:

Brand: Unknown, Year: 0
Brand: Toyota, Year: 2022

✅ 4. Example 3: Passing Current Object as Parameter

class Person {
String name;

Person(String name) {
this.name = name;
}

void display() {
System.out.println("Name: " + name);
}

void greet(Person p) {
System.out.println("Hello, " + p.name);
}

void greetCurrent() {
greet(this); // pass current object
}
}

public class Main {
public static void main(String[] args) {
Person p = new Person("Alice");
p.greetCurrent();
}
}

📌 Output:

Hello, Alice

✅ 5. Example 4: Calling Current Class Method

class Calculator {
int add(int a, int b) {
return a + b;
}

void displaySum(int a, int b) {
System.out.println("Sum = " + this.add(a, b)); // call method of current object
}
}

public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.displaySum(5, 10);
}
}

📌 Output:

Sum = 15

✅ 6. Key Points About this

Usage Purpose
this.variable Refers to current object’s instance variable
this() Calls another constructor in the same class
this.method() Calls another method of the current object
this as parameter Pass current object to another method or constructor
  • Helps resolve ambiguity between local and instance variables.

  • Can be used for constructor chaining but must be the first statement in a constructor.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *