"Security" in this case refers only to a programmer accidentally accessing a variable in a way different from 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 must always remember that denominador
can not be zero. But what if you forget? In time, nothing will happen ... But later, when another part of the code calls calcular
, it will give a division error by zero. In an hour you are not expecting, and it will look like it was the call to the calculation that caused the exception (making it harder to debug).
Now, let's 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 this case, you continue having to remember that you can not pass zero, but what if you forget, what changes? It changes that the exception will be thrown immediately when trying to assign the value, pointing to the exact line of code where the error occurred, so it is much easier to identify and correct the error.
Note that in the case of numerador
, it does not make sense to create getter and setter , you can either do it (by convention) or not. >
There are other benefits of having an extra layer of abstraction around your variables, for example by allowing you to change the internal representation of your class keeping your contract constant. These benefits are most significant when you want to have a stable API, or when programming on a large team where not everyone knows exactly what the other's code does.
In general the loss of performance in using one more method is paltry to the benefits that this technique brings. However, if you are always creating getters and setters "because I have learned that this is how it should be", but never or rarely need to validate a field, or refactor the implementation, or you have no one but yourself consuming your API, then the thing changes shape: not only do you spend more time writing a bunch of useless code, but the small losses in performance over the entire project pile up, (I doubt it, it looks like micro-optimization, but you'll know ... maybe Android's Java is not as optimized to handle this as the official JVM).
I think that's the reason behind this recommendation not to use on Android. Is the code "less secure"? Stay. But it's just a matter of being more careful when programming (and this is always "good practice"). Regarding the final product, this in itself does not cause any vulnerability in the code or anything like that.