Now I know that Java is purely passed-by-value, but are instance variables passed by reference?
Here’s what I mean (and I know this code is terrible, but it’s pseudo-code:
//Instance variables
private Object[] array = new Object[10];
array[4] = new Object[5];
//Private method
private Object ar(int x)
{
return array[x];
}
//Inside Main or some other method
ar(4)[0] = "Foo";
Now, would the first slot on the array in array[4] be changed to “Foo” because array is an instance variable?
To Clarify:
I know that EVERYTHING is passed by value. But we are talking about calling things contained in instance variables, please focus on that. Thanks.
Yes, the first slot in
array[4]will be changed to"Foo"In Java, everything is passed by value. When passing objects, the reference to the object is passed by value. For your example, the object contained in
array[4]is returned from thearmethod.