15
I would like an explanation with an example if possible on the reserved word this
in Java, I didn’t understand very well.
15
I would like an explanation with an example if possible on the reserved word this
in Java, I didn’t understand very well.
18
this
refers to the current object. Always read so it will become clearer to you: this object
Example:
public class Esse {
public static void main(String[] args) {
Esse esse1 = new Esse();
Esse esse2 = new Esse();
esse1.compara(esse2);
}
public void compara(Object aquele) {
if(this == aquele) {
System.out.println("mesmo objeto");
}
else {
System.out.println("Objetos diferentes");
}
}
}
Upshot:
Different objects
Inside the main(), when calling the method compares() the implicitly passed object that called it, in this case the object esse1
, then the comparison within the method returned false, because esse1 != esse2
Even, it is often said that this is the great difference of methods and functions, because when it is called a method its object is implicitly passed with it, and the same does not occur with functions in other languages, for example Pascal.
I said "it is customary to say" because it is what some people defend (including me), but although everything in Java is method and never functions, there are static methods that do not have an object attached to it, because static methods belong to the class and not to the object, therefore, it is not necessary that there is an object created for them to be accessed.
Example:
public static void teste() {
System.out.println(this); //ERRADO!!
}
The above code is invalid because the method does not belong to the object, but to the class, so the object this
is not passed to the method that was called.
The this
is also very commonly used to explain which variable we are talking about, whether it is the object variable or whether it is a local variable. Example:
public class Exemplo {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Look at the line this.id = id
, it is assigning the value of the local variable to the value of the variable of the object that was called the set method. If it was not possible to use the this
variables should have different names for the code to work correctly.
Browser other questions tagged java
You are not signed in. Login or sign up in order to post.
Thanks Math! Now I understand! Only a similar question appeared. I’ve seen in codes where they use the word
this
in a variable, for example > int numero; void metode(int numero) ? this.numero = numero; } This.numero refers to the variable of the object and not of the method?– Bruno Neuman
@Exact Brunoneuman! Thank you for remembering, I will increment and reply.
– Math
Thanks again :)
– Bruno Neuman
I use the
this
to send the object itself to a method, e.g..:ClasseChecagens.checaDados(this)
.– ptkato
@Patrick yes, it is more an application for the reserved word. Perhaps a little less common, I would say, but equally correct.
– Math