Reputation: 816
I know that if you pass a Class type as a parameter for a method the pointer will be passed and if you edit the parameter in the method you also edit the original object instance you passed (for example if a method named public void changeColor(Color color)
does the following code:
public void changeColor(Color color)
{
color = new Color(20, 100, 50);
}
and you called the value like so:
Color color = new Color(10, 20, 30);
System.out.println(color.toString());
changeColor(color);
System.out.println(color.toString());
both lines of the console should be different (java.awt.Color[r=10,g=20,b=30]
and java.awt.Color[r=20,g=100,b=50]
), thus the value is changed and the pointer is passed. On the other hand, if you use an int value, which is a primitive, you get different result:
public void changeNumber(int i)
{
i = 10;
}
and you called the value like so:
int n = 5;
System.out.println(n);
changeNumber(n);
System.out.println(n);
The console does not print 5
and 10
but on both lines says 5
. With that said, if I had this:
public class A
{
public int i;
public Color c;
public A(int n, Color color)
{
i = n;
c = color;
}
}
And in the runnable class...
public static void main(String[] args)
{
A a = new A(10, new Color(10, 40, 23));
changeA(a);
}
public static changeA(A varA)
{
varA.i = 54;
varA.c = new Color(40, 30, 264)
}
Will i inside the A class also act as if it were part of the pointer of A or will the value if a.i in the main method not change after I run changeA(a); If not, what can I do so it does change?
Upvotes: 0
Views: 1041
Reputation: 719679
Will i inside the A class also act as if it were part of the pointer of A or will the value if a.i in the main method not change after I run changeA(a)
The value of i
in the A
instance passed as the parameter to A
will change.
But I wouldn't say that it is acting "as if it were part of the pointer of A". The reference to A is a single immutable value. You are changing the state of the object it refers to.
By the way, this is not what "pass by reference" means, and it is not analogous to pass by reference as implemented in languages such as C# and C++, or the C hack of passing the address of a variable / member / whatever.
Upvotes: 0
Reputation:
This has been explain so many times before, and twice above, but this is how I like to explain it:
(Please note that I gloss the "internal details": I find that they are not needed to discus the semantics, but they should be understood at the end of the day. In fact, I'm not even going to discuss the evaluation strategies used except to say that Java uses call-by-value
and that passed objects have call-by-object-sharing
semantics, which is achieved internally by passing the reference of the object as a value.)
When a primitive value is passed a copy is made. (Although this is somewhat irrelevant to the next point because primitive values cannot be altered.)
When an object is passed (or assigned), that same object is passed (or assigned). No copy of the object occurs: unless re-assigned the the relevant parameter variable will evaluate to the same object as what was passed in (or null
, perhaps). Since an object is itself, then mutating the object in one location -- irregardless of what "variable name" it currently has -- changes the same object everywhere.
I you want a new, independent object, then create one :)
Happy coding.
Upvotes: 0