First of all ...
In Java there is no step by reference. The step by value (or by copy as some call) is mandatory.
You will say but when I pass an array by parameters and modify it from the method I pass it to, it changes, I am not passing a copy of the array
It seems that my argument fails, but I explain:
What you store in a non-primitive variable is not the object itself but an address or identifier of the object in the dynamic memory space. When you pass the variable parameters, you are passing a copy of that address.
The case you have proposed
You have created three objects of type
Gestor stores in its attributes the address of the objects
Ventana that you have passed through parameters. A copy of past objects has not been created.
What you do have is a copy of the address of the objects. If you add the following line to the Manager builder just after the ones you already have:
v = null;
You will only make the
v parameter no longer store the object address of type
Ventana . In class
otraCualquiera , variable
ventana will still have the correct address.
Therefore, the step IS ALWAYS by copy of the value, unlike, for example, C or C ++, where the step by reference is allowed. What you have to understand is that in the case of objects the value stored by a variable is an address or identifier of the object and not the object itself.
If what you want is to find a way to modify a variable of primitive type (like an int) from another method to which you pass it (something not very common, but useful in some recursive algorithms), a technique could be have the whole as an array of a single element, so it would be treated as an object and you would only pass a copy of its address (a full-fledged reference).
The usual thought is to store it as an object
Integer , but this class is immutable.