"Security" in this case refers only to a programmer accidentally accessing a variable in a way other than that intended by the class author (not necessarily a different programmer). Let me give you an example:
class Fração {
int numerador;
int denominador; // Não pode ser zero
public int calcular() { return numerador / denominador; }
}
If you can access the variables directly, you need to always remember that denominador
can’t be zero. But what if you forget? At the time, nothing will happen... But later, when another part of the code calls calcular
, He’s gonna make a division error by zero. At a time you are not waiting, and it will seem that it was the call to calculus that caused the exception (making it more difficult to debug).
Now, say you change the visibility of this variable to private
and create a getter and a Setter:
class Fração {
int numerador;
private int denominador; // Não pode ser zero
public int getDenominador() {
return denominador;
}
public void setDenominador(int denominador) {
if ( denominador == 0 )
throw new IllegalArgumentException("O denominador não pode ser zero!");
this.denominador = denominador;
}
public int calcular() { return numerador / denominador; }
}
In that case, you continues But if you forget, what changes? Changes that the exception will be thrown immediately when trying to assign the value by pointing to the exact line of code where the error occurred, so that it is much easier to identify and correct the error.
Note that in the case of numerador
, it makes no sense to create getter and Setter, you can do it (by convention) or not.
There are other benefits to having an extra abstraction layer around your variables, for example allowing you to change the internal representation of your class while keeping your contract constant. These benefits are most significant when you want to have a stable API, or when you program on a large team where not everyone knows exactly what each other’s code does.
In general, the loss of performance in using an extra method is derisory compared to the benefits that this technique brings. However, if you are always creating getters and setters "because I’ve learned that this is how it has to be, "but you never or rarely need to validate a field, or refactor the implementation, or you have no one but yourself consuming your API, then the picture changes: not only do you spend more time writing a bunch of useless code, how small losses in performance throughout the project accumulate, and can make a significant difference (I doubt, it seems to me micro-optimization, but you’ll know... maybe the Android Java is not as optimized to deal with this case as the official JVM).
I believe this is the reason behind this recommendation not to use on Android. Is the code "less secure"? It is. But it’s just a matter of being more careful when programming (and this is always "good practice"). In relation to the final product, this alone does not cause any vulnerability in the code or anything of that kind.
Could you post a link where this information says "Google says not to use, for reasons of performance (only in the case of Android)"? sff (ATTENTION is just curiosity)
– jsantos1991
Check out this link, address in general the concept of encapsulation/security http://www.guj.com.br/java/104592-heranca#564414 reply to @vinigody
– jsantos1991
Related: 1) Getters and Setters are mandatory or facilitators?; 2) Getters and setters are an illusion of encapsulation?; 3) Why getter and Setter methods are evil (Article in English).
– Math
If it weren’t for
Google diz para NÃO usar, por questões de perfomance (somente no caso do Android)
, I’d say it’s duplicate. As @jsantos1991 said, you could tell us where you got this information from?– Math
This one in an article on performance tips http://developer.android.com/training/articles/perf-tips.html It seems to me that proguard eliminates these methods when generating apk
– felipe.rce