To put it in a nutshell, reassigning a value (meaning with =
operator) to an existing reference does not change the object pointing by the original reference.
A big misunderstood in Java is that folks think that they are two types of variables:
- Primitives (like int, boolean etc...)
- References (like Integer, Boolean, custom objects etc...)
Java NEVER uses references. The word Reference is misnomer.
Java only uses Pointers instead for manipulating objects.
To better understand the shade: http://javadude.com/articles/passbyvalue.htm
Regarding your case, even though you skipped the Java Naming Conventions (but it's another subject), you could solve your "issue" by doing:
void SetVar(Integer value) {
this.A = value;
}
Indeed, if you pass A
as a local parameter (as you did), this local parameter will represent a copy of the A
reference since Java is only focused on passed-by-value
. So changing it does not affect the initial reference.