Back to Java
2026-03-136 min read

this (Java)

Learn this (Java) step by step with clear examples and exercises.

Title: Mastering 'this' Keyword in Java - A full guide

Why This Matters

In Java, the this keyword is a fundamental aspect of object-oriented programming (OOP). It allows you to access current object's methods and variables, create new objects using constructors, and avoid naming conflicts. Understanding 'this' is crucial for writing efficient and error-free code, especially when dealing with complex classes and multiple instances of an object.

Prerequisites

Before delving into the this keyword, make sure you have a solid grasp of the following concepts:

  • Java basics (variables, methods, classes)
  • Object-oriented programming (OOP) principles
  • Constructors and their usage in Java
  • Static members (variables and methods)

Core Concept

Understanding 'this'

The this keyword refers to the current object instance in a class. It can be used for various purposes:

  1. Accessing current object's variables (fields):
public class Car {
String color;
int speed;

public void setColor(String color) {
this.color = color; // Accessing the current object's 'color' variable using 'this'
}
}
  1. Invoking current object's methods:
public class Car {
public void accelerate() {
speed += 10;
this.move(); // Invoking the current object's 'move' method using 'this'
}
}
  1. Creating new objects using constructors:
public class Car {
String color;
int speed;

public Car(String color) { // Constructor with a single parameter
this.color = color;
}
}
  1. Avoiding naming conflicts:
class Person {
int age;

void setAge(int age) {
this.age = age; // Accessing the current object's 'age' variable to avoid a local variable with the same name
}
}

Different forms of 'this'

  • Implicit this: When you don't explicitly use 'this', Java implicitly uses it.
  • Explicit this: You can use 'this' explicitly to refer to the current object instance.
public class Car {
String color;
int speed;

public void setColor(String color) {
this.color = color; // Implicit 'this'
this.setSpeed(0); // Explicit 'this' to invoke the current object's method
}
}

'this' in constructors

In Java, you can use this inside constructors to:

  1. Call another constructor (overloading):
public class Car {
String color;
int speed;

public Car() { // Default constructor
this("white", 0); // Invoking the parameterized constructor using 'this'
}

public Car(String color) { // Parameterized constructor with a single parameter
this.color = color;
}

public Car(String color, int speed) { // Parameterized constructor with multiple parameters
this.color = color;
this.speed = speed;
}
}
  1. Invoke superclass's constructor:
public class Vehicle {
String type;

public Vehicle(String type) { // Superclass constructor
this.type = type;
}
}

public class Car extends Vehicle {
String color;
int speed;

public Car() { // Subclass constructor calling superclass constructor using 'this'
super("vehicle");
}
}

'this' and static members

  • Implicit this cannot be used inside static methods or blocks to access non-static variables or invoke non-static methods.
  • Explicit this can only be used within an instance method to call another instance method with the same name in the current object instance.
public class Car {
public static void main(String[] args) {
Car car1 = new Car(); // Creating a new object 'car1'
car1.accelerate(); // Invoking the 'accelerate' method on 'car1' using implicit 'this'
car1.move(); // Invoking the 'move' method on 'car1' using implicit 'this'
}

public void accelerate() {
speed += 10;
}

public void move() {
System.out.println("Moving...");
}

public void staticMethod() { // Static method with no implicit 'this'
System.out.println("This is a static method.");
}
}

Worked Example

In this example, we will create a Person class with methods to set name and age, and display the person's details. We will also demonstrate the usage of this keyword in various scenarios.

public class Person {
String name;
int age;

public Person(String name) { // Constructor with a single parameter
this.name = name;
}

public void setAge(int age) {
this.age = age; // Accessing the current object's 'age' variable using 'this'
}

public void displayPersonDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}

public class Main {
public static void main(String[] args) {
Person person1 = new Person("John"); // Creating a new object using the constructor
person1.setAge(25); // Setting the age of the current object 'person1'
person1.displayPersonDetails(); // Displaying the details of the current object 'person1'
}
}

Common Mistakes

  1. Forgetting to initialize variables in constructors:
public class Car {
String color;
int speed;

public Car(String color) { // Constructor with a single parameter
color = color; // Forgot to use 'this' to access the current object's variable
}
}
  1. Using this inside static methods:
public class Car {
public static void accelerate() { // Static method with no implicit 'this'
speed += 10; // Trying to access a non-static variable 'speed' using 'this'
}
}
  1. Using this inside nested classes:
public class Car {
public static class Engine {
void startEngine() {
this.Car.start(); // Trying to access a method 'start' from the outer class 'Car' using 'this'
}
}
}
  1. Using this inappropriately with static members:
public class Car {
public static String color;

public void setColor(String color) {
this.color = color; // Trying to access a static variable 'color' using 'this'
}
}

Practice Questions

  1. Create a Rectangle class with variables for width and height, and methods to calculate area and perimeter. Use the this keyword to access the current object's fields.
  2. Write a constructor for the Person class that accepts name, age, and gender as parameters. Also, create a method called getDetails() that displays the person's details using the System.out.println() statement.
  3. Create a Car class with variables for color, speed, and model. Write a constructor that accepts color, speed, and model as parameters. Use the this keyword to initialize the fields and call another constructor if necessary.
  4. Modify the Person class from the worked example to include a method called increaseAge(), which increments the person's age by one year using the this keyword.
  5. Create a Square class that extends the Rectangle class and overrides the methods for area and perimeter calculations specific to squares. Use super and this keywords appropriately in the constructor and methods.
  6. Write a static method called getCarCount() within the Car class, which increments a static counter each time a new car object is created using the constructor. Use the this keyword to call this method from the constructor.
  7. Create a Shape interface with a method called area(). Implement this interface in both Rectangle and Square classes, and calculate their respective areas using the this keyword.

FAQ

What happens when you don't use 'this' in Java?

  • When you don't explicitly use 'this', Java implicitly uses it to access current object's methods and variables.

Can I use 'this' inside static methods or blocks?

  • No, 'this' cannot be used inside static methods or blocks because they do not have a reference to the current object instance.

How can I use 'this' in constructors to call other constructors?

  • You can use this(...) inside constructors to call another constructor with appropriate arguments.

What is the purpose of using 'this' while invoking a method on the current object instance?

  • Using 'this' explicitly helps you to invoke a method on the current object instance when there are multiple methods with the same name in the class or superclass hierarchy.

How can I use 'this' to access static members from an instance method?

  • You cannot directly access static members using 'this'. Instead, use the class name (e.g., MyClass.staticVariable) or call a static method that modifies the static variable.

Can I use 'this' to create a new object of the same class within a method?

  • Yes, you can create a new instance using the constructor and assign it to a local variable within a method. However, this new object will not have access to the current object's state unless explicitly passed as an argument or returned from the method.
this (Java) | Java | XQA Learn