I was just reading this stack question:
Is Java "pass-by-reference" or "pass-by-value"?
And I was wondering if it is considered bad programming practice to use a method to alter an object by it's address. I question this because it is no doubt a confusing property of Java.
So you have this code taken from the above question:
Person person;
person = new Person("Tom");
changeName(person);
//I didn't use Person person below as an argument to be nice
static void changeName(Person anotherReferenceToTheSamePersonObject) {
anotherReferenceToTheSamePersonObject.setName("Jerry");
}
It really seems like this is something to avoid in programming because of its confusing nature. Is this typically an acceptable method of Java programming?
(The classic alternative of course being to call a person.changeName() function with the new name as the parameter.)
If this is not appropriate for Stack, by all means I'll remove it. I was just interested in how this ability is used in the real world. Is it condoned in a professional environment? Thanks!