Reputation: 11
I have a quick question regarding local variables in Java:
If, upon declaring a local variable, I point it at an instance variable, does the local variable then act as a reference to that instance variable, or does act like a temporary deep copy?
In other words, if I invoke a modifier method on the newly initialized local variable, will the local variable act as a reference and invoke the modifier on the instance variable, will it modify a copied version pointed to by the local variable, or can modifier methods not be invoked on local variables?
Eg.
public static > boolean isSorted(Stack s) {
...(bunch of code)
else if(s instanceof DynamicArrayStack) { ...(bunch of code)
DynamicArrayStack tempStack = (DynamicArrayStack) s; E elem = (E) tempStack.pop();
...(bunch of code) } ...(bunch of code) }
Will invoking pop() on 'tempStack' cause pop() to be invoked on the instance of Stack pointed to by the parameter 's' as well? Or will it just affect the contents of my 'tempStack'?
Upvotes: 1
Views: 192
Reputation: 26228
If, upon declaring a local variable, I point it at an instance variable, does the local variable then act as a reference to that instance variable, or does act like a temporary deep copy?
The first option. Java doesn't implicitly make deep copies of objects. What you have is a temporary reference to the instance object, which the instance variable also references.
Upvotes: 5
Reputation: 1612
In your example, "tempStack" and "s" both point to the same instance of DynamicArrayStack (if it is a DynamicArrayStack).
In order for "tempStack" to function as it appears the programmer expects it to, you would need to call some kind of clone/copy method on "s" first.
The Object class (from which all other classes are necessarily extended) provides the "clone" method for this purpose, but you should be warned that it is considered bad practice to use this method because it doesn't provide a deep copy and can introduce some funky bugs as a result; you should use a copy constructor instead.
Upvotes: 2
Reputation: 2040
Yes. Remember java works with pointers not values. so when you say
tempStack = s;
tempStack actually gets the value of s, but in java this value is a pointer.
So now both tempStack and s point to the same data.
if you do tempStack.pop() it is as doing s.pop() or vice-versa.
Upvotes: 0
Reputation: 15995
It will change s and tempStack (because is the same element referenced). But it will not change the object that was passed in isSorted, given that parameters are passed by value in Java.
So if you have:
Stack myStack = new Stack()
...
isSorted(myStack);
The object myStack will not be modified. But if in your method isSorted s and tempStack will.
I mean:
s.pop()
will be the same than
tempStack.pop()
Both variables reference the same object so both will modify the same object.
This would clarify a little bit more.
Best regards,
Upvotes: 0
Reputation: 1892
Short answer: yes, s
and tempStack
are two variables referencing the same instance.
Calling tempStack.pop()
is the same as calling s.pop()
, since both reference the same object.
Upvotes: 0
Reputation: 81907
Its all references. So the pop you mentioned will affect s as well since it is the same object.
Upvotes: 0