When I pass an object to a method via a parameter, is the object passed by value or reference? The question comes from this code:
int a = 2;
cambiar(a);
System.out.println(a);
public static void cambiar(int c) {
c = 10;
}
In the code above I already knew that I was not going to modify a a
in any way unless the method returned an int and saved it. But in the following code what happens?
class otraCualquiera {
Partidas partidas = new Partidas();
Ventana ventana = new Ventana();
Gestor gestor = new Gestor(ventana,partidas);
}
class culquiera {
private Ventana view;
private Partidas play;
public Gestor(Ventana v, Partidas p) {
view = v;
play = p;
}
}
The main question, why can I directly modify the other classes from the class anyone? ( view.setTitle("View sería un jframe")
) and from the first code I showed I can't modify the int
?
Before anything...
In Java there is no such thing as passing by reference. Passing by value (or by copy as some call it) is mandatory.
You will say but when I pass an array by parameters and I modify it from the method to which I pass it, it changes, I am not passing a copy of the array
It seems that my argument fails, but I'll explain:
What you store in a non-primitive variable is not the object itself but an address or identifier of the object in dynamic memory space. When you pass the variable as parameters, you are passing a copy of that address.
The case you have proposed
You have created three objects of type
Partidas
,Ventana
andGestor
.The object
Gestor
stores in its attributes the address of the objectsPartidas
andVentana
what you have passed to them as parameters. A copy of the passed objects has not been created.What you do have is a copy of the objects address. If you add the following line to the Manager constructor right after the ones you already have:
You'll just make the parameter
v
no longer store the address of the object of typeVentana
. In the classotraCualquiera
, the variableventana
will still have the correct address.Thus, passing IS ALWAYS by copy of the value, unlike in, say, C or C++, where passing by reference is allowed. What you have to understand is that in the case of objects, the value that a variable stores is an address or identifier of the object and not the object itself.
If what you want is to find a way to be able to modify a variable of primitive type (like an int) from another method that you pass it to (something not very common, but useful in some recursive algorithms), a technique could be to have the integer as a single-element array, then it would be treated as an object and you would only pass a copy of its address (a full-fledged reference).
The usual thinking is to store it as an object
Integer
, but this class is immutable.The brief points to remember here are that in Java:
The second point is one that causes a lot of confusion. In Java, when we have the following situation:
...we popularly say that the value of the variable is an "object," but this way of speaking is an everyday simplification—a convenient imprecision. If we want to get to the bottom of this question, we have to understand that the way Java works, the value of
saludos
is not the object, but a reference to the object. The variable, the reference, and the object are three related but separate things, and you have to look carefully at which operations affect which, and in which way. For example:saludos = "¡Saludos de nuevo!";
) changes the value of the variable. But the value of the variable in this case is a reference, not an object. Assignment does not access objects—it works strictly with variables and their values.a == b
ora != b
, look at the current values of the variables. When these values are references, then they look at these references but do not access their objects.null
it is a reference, not an object. It is a reference without an object, by definition.saludo.length()
) or member reference (objeto.variable
) observes the current value of the variable, and accesses the object that corresponds to this reference.All the examples you give can be answered by strictly applying these rules. For example:
In this example you give, contrary to what you say, there is no direct modification of another class. What you are actually doing is:
view
;setTitle
of this object, passing it a reference to the object"View sería un jframe"
.This in itself does not change anything; if there is modification, it is indirectly, because the method
setTitle
modifies something directly or indirectly causes such modification through calls to other methods.If you look at my list again, item #1 is the only mechanism that actually modifies the value of a variable. None of the other points have the "power" to modify anything directly. Although saying how you say
view.setTitle("View sería un jframe")
"modifiesview
" is a popular and simplified way of describing what happens, in reality this is not what happens when we look strictly at how things work.There are no references in Java. Parameters are passed by copy (basic type variables of the language or by copying the address where they are stored) THIS IS NOT PASSING BY REFERENCE !!!!!!!. Passing by reference is creating an alias of a variable or object that has a different name but REFERENCES to the same memory space. For example:
MEMORY SPACE 10 -> We have the person1 object stored in this space. The reference would be -> create a personReference object that is an alias of person1.
In conclusion, a reference is like calling a memory space in various ways without using additional memory resources.
In Java copy is used. of the content of the variable or, in the case of an object, of the memory address in which it is located. In both cases the content of the variable is copied to another variable OR the memory address of an object to another object (It is what is called a pointer in C) the difference is that Java handles it in a dedicated way instead in C it does the programmer directly.