OBJECT-ORIENTED PROGRAMMING

Java this keyword

The keyword this refers to the current object: the instance whose constructor or method is running.

What does this mean?

Every instance method and constructor runs for a specific object. Inside it, this is a reference to that object. Through this you can therefore access its attributes and methods.

this cannot be used in a static context.

static method belongs to the class rather than a specific object, so it has no current object.

An attribute and a parameter with the same name

A constructor or method parameter can have the same name as a class attribute. In that case, this.atributas explicitly identifies the object's field:

public class Knyga {
    String pavadinimas;
    int puslapiuSkaicius;

    Knyga(String pavadinimas, int puslapiuSkaicius) {
        this.pavadinimas = pavadinimas;
        this.puslapiuSkaicius = puslapiuSkaicius;
    }

    public static void main(String[] args) {
        Knyga knyga = new Knyga("Mažasis princas", 96);
        System.out.println(knyga.pavadinimas);
        System.out.println(knyga.puslapiuSkaicius);
    }
}
Console output

Mažasis princas
96

  1. When the object is created, the constructor receives a title and page count.
  2. The parameter's pavadinimas name matches the object's attribute name.
  3. this.pavadinimas explicitly means the attribute of the object being created.
  4. The plain right-hand pavadinimas means the constructor parameter.
  5. Argument values are thus saved in the object and displayed in the console later.
Read it as a simple sentence.

this.pavadinimas = pavadinimas means: “assign the pavadinimas parameter's value to this object's title”.

What happens if you omit this?

public class Knyga {
    int puslapiuSkaicius;

    Knyga(int puslapiuSkaicius) {
        puslapiuSkaicius = puslapiuSkaicius;
    }

    public static void main(String[] args) {
        Knyga knyga = new Knyga(96);
        System.out.println(knyga.puslapiuSkaicius);
    }
}
Console output

0

  1. The constructor receives the argument 96.
  2. Parameter puslapiuSkaicius hides the object's attribute with the same name.
  3. The line puslapiuSkaicius = puslapiuSkaicius assigns the parameter to itself.
  4. The object's attribute is never accessed and never receives the value 96.
  5. int attribute's default value is 0, so the console shows zero.
The correct line would be this.puslapiuSkaicius = puslapiuSkaicius;

this tells Java to change the object's attribute on the left.

Calling another constructor with this()

The form this(...) calls another constructor in the same class. This provides default values without repeating initialization code:

public class Knyga {
    String pavadinimas;
    int puslapiuSkaicius;
    boolean perskaityta;

    Knyga(String pavadinimas) {
        this(pavadinimas, 0, false);
    }

    Knyga(String pavadinimas, int puslapiuSkaicius, boolean perskaityta) {
        this.pavadinimas = pavadinimas;
        this.puslapiuSkaicius = puslapiuSkaicius;
        this.perskaityta = perskaityta;
    }

    public static void main(String[] args) {
        Knyga knyga = new Knyga("Užrašų knygelė");
        System.out.println(knyga.pavadinimas);
        System.out.println(knyga.puslapiuSkaicius);
        System.out.println(knyga.perskaityta);
    }
}
Console output

Užrašų knygelė
0
false

  1. The object is created by passing only the title Užrašų knygelė.
  2. The first constructor calls the second using this(pavadinimas, 0, false).
  3. This supplies default values for the missing data: 0 pages and false.
  4. The second constructor stores all three values in the current object's attributes.
  5. We get a fully initialized object even though we initially passed only one argument.
this(...) must be the first constructor statement.

Java requires this so the object is properly initialized before other actions.

Calling the current object's method

Through this can also call another method on the current object:

public class Knyga {
    String pavadinimas = "Mažasis princas";

    void rodytiPavadinima() {
        System.out.println(this.pavadinimas);
    }

    void rodytiInformacija() {
        this.rodytiPavadinima();
        System.out.println("Knyga paruošta skaityti");
    }

    public static void main(String[] args) {
        Knyga knyga = new Knyga();
        knyga.rodytiInformacija();
    }
}
Console output

Mažasis princas
Knyga paruošta skaityti

  1. The method rodytiInformacija() runs on a specific knyga object.
  2. Inside it, this.rodytiPavadinima() calls another method on the same object.
  3. That method uses this.pavadinimas to display the current object's title.
  4. The program returns to rodytiInformacija() and displays the second text.
  5. In this case, this is optional but makes it clear that the current object is being used.

Passing the current object

this is an object reference, so it can be passed to another method as an argument:

public class Knyga {
    String pavadinimas = "Mažasis princas";

    void pridetiIBiblioteka() {
        Biblioteka.prideti(this);
    }

    public static void main(String[] args) {
        Knyga knyga = new Knyga();
        knyga.pridetiIBiblioteka();
    }
}

class Biblioteka {
    static void prideti(Knyga knyga) {
        System.out.println("Pridėta knyga: " + knyga.pavadinimas);
    }
}
Console output

Pridėta knyga: Mažasis princas

  1. A specific instance is created: Knyga object with the title Mažasis princas.
  2. Through this object, the method is called: pridetiIBiblioteka().
  3. Inside the method, this refers to that exact book object.
  4. The object is passed as an argument to the static method Biblioteka.prideti().
  5. The library method receives the book, reads its title and displays it in the console.

Common forms of this

Ways to use Java this
The formPurpose
this.atributasAccess the current object's attribute
this.metodas()Call the current object's method
this(...)Call another constructor in the same class
kitasMetodas(this)Pass the current object as an argument