I am a little confused with Java “All passings by value” concept.
Consider the following code:
class Test {
Integer A;
String B;
...
void SetVar(Object??? var, Object value) {
// Set A variable to the value (considering that it's possible)
}
}
can I code the SetVar function in the way that the following code sets A to 2 and B to Hi??
void Init() {
SetVar(this.A, 2);
SetVar(this.B, "Hi");
}
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:
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:
Indeed, if you pass
Aas a local parameter (as you did), this local parameter will represent a copy of theAreference since Java is only focused onpassed-by-value. So changing it does not affect the initial reference.