This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
In the above example, we are creating an object of type ABC inside main() method and calling a method on that object. Now, within the method, we are again creating an object of type ABC, using the same reference variable name 'abc'. I know that the values of 'x' and 'y' would be 10 and 20 after printing. Now, my question is:
01) We are using same reference variable to refer to 2 different objects. As soon as the second object is created, reference to the first object will be dropped and will be created for the second object, since we are using the same reference variable. Is it so?
02) If it is so, then how we are getting the values in first object (x=10, y=20) and not the new pair of values (x=30, y=40)
Please use the code button. Fortunately I can apply it to your code, and you can see how much better it looks
I think you have a misunderstanding about pass‑by‑reference and pass‑by‑value. There is a long discussion about it here; have a look at that when you have a week to spare.
The rules about pass‑by‑reference are very simple in Java: it doesn’t exist. If it used pass‑by‑reference, you could write something like thisBut it doesn’t, so you can’t. All that code can be compiled and executed in Java, but it doesn’t print 234. The original f reference, outside the method, remains unchanged. So it prints Foo 123. The only Foo references changed are those in the method.
Remember, when people tell you that reference types are pass‑by‑reference and primitives are pass‑by‑value, those people are telling you rubbish.
Variables have a 'scope'. This is basically where the object can be seen and where it can't.
The 'abc' variable in the main method can only be seen in the main method, and the 'abc' variable in your method_01 can only be seen in the method_01.
When your main method calls method_01, you get a new, distinct variable. Java knows which is which.
So, your statement "As soon as the second object is created, reference to the first object will be dropped" is simply not true.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Jan 21, 2013
Thanks Ritchie and Fred.
Continuing the discussion with Fred:
As you mentioned Fred - So, your statement "As soon as the second object is created, reference to the first object will be dropped" is simply not true.
I am confused a bit now. Since we are using the same reference variable name to refer to 2 objects, how can single variable can point to 2 different objects?
After reading a little about the memory concepts, I know now that each method has its own stack. So, main() method will have its own stack and we are creating 1st object inside main() method. Next, method_01() will have its separate stack and 2nd object is created from this method. So, has it got to do something with memory? The 2 reference variables will simply exist in different stacks (even though they are of same name) and will keep on pointing on different objects on the heap?
Sachin Dravid wrote:Since we are using the same reference variable name to refer to 2 objects, how can single variable can point to 2 different objects?
You might be using the same variable name, but they are not the same variable. You have two separate variables pointing to two separate objects.
Joined: Jan 21, 2013
Thanks Joanne for the reply.
But that is what is puzzling me. Can you please elaborate a bit about memory/stack? Like where will the 2 reference variables land?
Joined: Aug 05, 2005
As both variables are local variables (declared inside a method rather than as part of the class declaration), they will both be on the stack.
Joined: Aug 05, 2005
Actually I've just taken another look at your code.
Sachin Dravid wrote:02) If it is so, then how we are getting the values in first object (x=10, y=20) and not the new pair of values (x=30, y=40)
That is because you don't set x to 30 or y to 40. You assign those values to variables called a and b, but those variables don't appear to be declared anywhere, so that code won't actually compile. Maybe you should post the actual code you are using (or at least show us the declarations of a and b.
Then at the time of the execution of the method "bar", both "foo" and "bar" will have their frames on actually the same stack (that of the executing thread).
The two variables called "abc" are different, they have different scopes (namely of their declaring methods respectively), so they will occupy different places in the stack:
each inside the frame of its declaring method.
But these are the details of the implementation, it might be better to simply think of them as of two different variables, albeit having accidentally the same name,
but being of different scopes, and hence being different.