3
It’s just that sometimes I think there’s no need to use the this
, but as I just started to learn Java, I want someone to answer me when it will really be necessary to use the operator this
?
3
It’s just that sometimes I think there’s no need to use the this
, but as I just started to learn Java, I want someone to answer me when it will really be necessary to use the operator this
?
10
4 situations:
Reference a class instance variable unambiguously
It is possible but some people recommend to avoid using where it is not ambiguous. So if an instance variable of the class does not conflict with a variable name or method parameter, it is common to avoid use. He is regarded as existing implicitly.
Use as argument of a method of another class that will call when you want to pass the current object itself
This is useful when you do not have or cannot pass a specific variable as argument, the intention is to pass the object itself, which is the name of the current class object? this
. Further below explain why this is the variable.
Accessing an instance within an internal class:
public class Externa {
private int x;
public class Interna {
private int x;
public int exemplo() {
return Externa.this.x;
}
}
}
Calling a constructor of the object itself
There are problems in calling constructors within constructors. This is a way to indicate that what you are calling is the constructor. There is a special treatment. This call should be the first line of the constructor, there can be no other code before. More in detail at What makes this() alone in the builder?
Note that the this
in the first two cases they function practically as a variable. What many people don’t know is that the method is just a function like any other that has a hidden parameter called this
. It is only a syntactic sugar. This variable can be used in various ways. So:
class Tipo {
private int variavel;
public void exemplo(int x) {
variavel = x;
}
}
That’s what it’s all about:
class Tipo {
private int variavel;
public void exemplo(Tipo this, int x) {
this.variavel = x;
}
}
And this is an example of the first described use. I speak in more detail in C# (is the same thing).
The third way is very similar to this case.
The second way would be:
class Tipo {
public void exemplo(Tipo this, int x) { //já escrito sem o açúcar sintático
metodoDeOutraClasse(this, x); //passa o objeto atual e o parâmetro x
}
}
Obviously in that case the signature of the method would be something like this metodoDeOutraClasse(Tipo objeto)
. This method most likely belongs to another class, it would not make sense in the same.
In the fourth case the this
works especially for the language. It uses this name convention to indicate that it is calling a constructor.
class Tipo {
public Tipo(int x) {
System.out.println(x);
}
public Tipo() {
this(1);
}
}
7
The only case where you accurate use the this
is when you have a scope variable (or parameter) with the same name as an attribute (or class variable) and you need to differentiate them.
A common example
public class Cliente {
private String nome;
public void setNome(String nome){
this.nome = nome;
//perceba que nome se refere ao parâmetro e this.nome se refere ao atributo
}
}
Other than that, you will never be "obligated" to use it. This is a subject to the style you each use to encode.
3
Explaining in a less formal way, the this
refers to the current object. For example: you have a ball object with a color property, you can access the color property with this.cor
Already the method this()
with or without parameters refers to the constructor of its class. This can only be used inside another constructor.
Example taken from this website.
If we create a method that receives an argument called linked that we want to assign to the class attribute, which is also called bound, we must differentiate both by showing to whom each belongs. Like
this
refers to the context used, so we use it to identify which bound will be the class attribute and connected without thethis
refers to the method parameter. What would result in this:public class TV { //atributos int tamanho; int canal; boolean ligada; // método contrutor com parâmetro TV(boolean ligada) { this.ligada = ligada; /** * Onde this.ligada é o atributo * e ligada é o valor do parâmetro */ } }
1
An easy way to understand is:
When you have two variables in different scopes: one local (in a method) and the other global (in the class). When you need to reference the global you use this, ex: this.var
.
The staff set a good example with the methods. We use a lot in getters and setters methods.
Browser other questions tagged java oop
You are not signed in. Login or sign up in order to post.
Related: http://answall.com/questions/109845/o-que-faz-o-this-sozinho-constructor, http://answall.com/questions/108957/qual-a-import%C3%A2ncia-do-palavra-this, http://pt.stackoverflowcom/questions/118962/pra-que-serve-this-em-java.
– Igor Contini