W3docs

Java this Keyword

Reference the current object in Java with the this keyword — disambiguating fields, chaining constructors, and method calls.

Inside an instance method or constructor, this refers to the object the method is being called on. It's an implicit parameter that the JVM passes for you. Most of the time you don't have to mention it — bare names already resolve through this — but a handful of situations force you to write it explicitly. This chapter covers them.

What this is

When you call dog.bark(), the JVM secretly passes dog as the first argument. Inside bark, that argument is named this. So:

public class Dog {
  String name;
  void bark() {
    System.out.println(this.name + " says woof");   // this == dog
  }
}

Dog rex = new Dog();
rex.name = "Rex";
rex.bark();    // inside, this == rex

You can omit this. and write just name — Java looks the bare identifier up against the current scope, finds no local with that name, then walks to the instance fields, finds name, and resolves it as this.name. The two forms compile to the same bytecode.

Use 1 — disambiguating a parameter from a field

The most common reason to write this. explicitly is when a parameter or local variable has the same name as a field:

public class Point {
  int x, y;
  public Point(int x, int y) {
    this.x = x;        // field = parameter
    this.y = y;
  }
}

Without this., x = x; would assign the parameter to itself and leave the field at 0. The compiler doesn't warn — it's a valid statement — so this is a popular silent bug.

You could rename the parameters (int newX, int newY) to avoid the collision, but matching parameter names to field names is by far the more readable convention and so the this. pattern is everywhere in Java code.

Use 2 — passing the current object

Sometimes a method needs to hand this to another method or constructor as an argument:

public class Order {
  List<Item> items = new ArrayList<>();
  void register(Tracker t) {
    t.watch(this);          // pass myself to the tracker
  }
}

There's no other way to refer to the current object as a value, so this is required here.

A common variant is a fluent builder where each setter returns this so calls can be chained:

public class Url {
  String scheme, host, path;
  public Url scheme(String s) { this.scheme = s; return this; }
  public Url host(String h)   { this.host   = h; return this; }
  public Url path(String p)   { this.path   = p; return this; }
}

Url u = new Url().scheme("https").host("w3docs.com").path("/learn-java");

Use 3 — this(...) to call another constructor

Inside a constructor, this(args) calls another constructor on the same class — see constructors. It must be the first statement, and a constructor can only delegate to one other:

public Rectangle()                  { this(1, 1); }
public Rectangle(double side)       { this(side, side); }
public Rectangle(double w, double h){ this.width = w; this.height = h; }

This is different from the this. qualifier — this(...) with arguments is the constructor-call form; this.foo (or just this by itself) is the reference form.

When this is illegal

this only exists in instance contexts. From a static method or initializer, there's no current object, so writing this is a compile error:

public class Counter {
  static int total;
  static void reset() {
    this.total = 0;     // ERROR: cannot use this in a static context
  }
}

main is static, which is why you can't refer to instance fields directly inside it — you must first create an object.

this vs implicit field access

If there's no name collision, this. is purely stylistic:

double area()      { return Math.PI * radius * radius; }
double areaThisly(){ return Math.PI * this.radius * this.radius; }

Both work. The implicit form is more common; some codebases use this. everywhere for consistency or to make the receiver visible at a glance. Either is fine — pick one and apply it within a file.

Inner classes and the outer this

A non-static inner class has an implicit reference to its enclosing instance. From the inner class, this refers to the inner object; the outer instance is Outer.this:

public class Outer {
  int x = 1;
  class Inner {
    int x = 2;
    void demo() {
      System.out.println(this.x);          // 2  — Inner's x
      System.out.println(Outer.this.x);    // 1  — Outer's x
    }
  }
}

This comes up only in nested-class scenarios, covered in nested classes and the chapters that follow.

A worked example

java— editable, runs on the server

What's next

this is one of the two implicit references in Java; the other, super, comes up once you have inheritance. Before that, though, we need to talk about visibility — who can see and call your class's methods and fields. Continue to access modifiers.

Practice

Practice

A constructor is written as public Point(int x, int y) { x = x; y = y; }. What's the bug?