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);
}
}Mažasis princas
96
- When the object is created, the constructor receives a title and page count.
- The parameter's
pavadinimasname matches the object's attribute name. this.pavadinimasexplicitly means the attribute of the object being created.- The plain right-hand
pavadinimasmeans the constructor parameter. - Argument values are thus saved in the object and displayed in the console later.
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);
}
}0
- The constructor receives the argument
96. - Parameter
puslapiuSkaiciushides the object's attribute with the same name. - The line
puslapiuSkaicius = puslapiuSkaiciusassigns the parameter to itself. - The object's attribute is never accessed and never receives the value
96. intattribute's default value is0, so the console shows zero.
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);
}
}Užrašų knygelė
0
false
- The object is created by passing only the title Užrašų knygelė.
- The first constructor calls the second using
this(pavadinimas, 0, false). - This supplies default values for the missing data:
0pages andfalse. - The second constructor stores all three values in the current object's attributes.
- 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();
}
}Mažasis princas
Knyga paruošta skaityti
- The method
rodytiInformacija()runs on a specificknygaobject. - Inside it,
this.rodytiPavadinima()calls another method on the same object. - That method uses
this.pavadinimasto display the current object's title. - The program returns to
rodytiInformacija()and displays the second text. - In this case,
thisis 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);
}
}Pridėta knyga: Mažasis princas
- A specific instance is created:
Knygaobject with the title Mažasis princas. - Through this object, the method is called:
pridetiIBiblioteka(). - Inside the method,
thisrefers to that exact book object. - The object is passed as an argument to the static method
Biblioteka.prideti(). - The library method receives the book, reads its title and displays it in the console.
Common forms of this
| The form | Purpose |
|---|---|
this.atributas | Access 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 |