hi
I am sorry to post such a silly question but
In the following pseudo-code , does the b gets the referance, copy or
original object and what happens to the local variable a in func()
Object b = func();
Object func(){
object a = a();
a.setBlah(3);
a.setFoo(5);
return a;
}
Jan Thomä - 30 Nov 2006 09:33 GMT
> In the following pseudo-code , does the b gets the referance, copy or
> original object and what happens to the local variable a in func()
[quoted text clipped - 8 lines]
> return a;
> }
b gets assigned a reference to a.
Best regards,
Jan

Signature
__________________________________________________________
insOMnia - We never sleep...
http://www.insomnia-hq.de
Ian Wilson - 30 Nov 2006 10:44 GMT
> hi
>
> I am sorry to post such a silly question but
>
> In the following pseudo-code , does the b gets the referance, copy or
> original object
Reference
> and what happens to the local variable a in func()
Here's how I understand it ...
The variable "a" holds a reference to an object. "a" and the object are
separate things. After the return statement is executed, the variable
"a" goes out of scope (and presumably gets garbage collected) however
the object that "a" referred to still exists and is not garbage
collected because there is still a reference to it (in "b").
> Object b = func();
>
[quoted text clipped - 5 lines]
> return a;
> }
Lew - 02 Dec 2006 18:40 GMT
sakcee@gmail.com wrote:
>> and what happens to the local variable a in func()
> Here's how I understand it ...
>
[quoted text clipped - 3 lines]
> the object that "a" referred to still exists and is not garbage
> collected because there is still a reference to it (in "b").
The variable is not garbage collected; only objects are garbage collected.
- Lew
RedGrittyBrick - 02 Dec 2006 19:34 GMT
> sakcee@gmail.com wrote:
>>> and what happens to the local variable a in func()
[quoted text clipped - 10 lines]
>
> - Lew
Thanks for the correction.
Nigel Wade - 30 Nov 2006 11:46 GMT
> hi
>
> I am sorry to post such a silly question but
It's not a silly or stupid question. It's actually a very important question,
and fundamental to the understanding of objects, and references to objects. You
do need to understand this particular point to understand how and why certain
things work the way they do in Java.
> In the following pseudo-code , does the b gets the referance, copy or
> original object and what happens to the local variable a in func()
[quoted text clipped - 8 lines]
> return a;
> }
The short answer is 'b' gets a reference to the object created within the method
(assuming the unspecified method a() creates an object and returns a reference
to it...). The local variable 'a' disappears when the method returns.
The variable 'a' only holds a reference to an object (whatever is returned by
a()). What gets returned by func() is a copy of that *reference*, not a copy of
the object it refers to. So, when func() returns, a copy of 'a' is returned and
stored in 'b'. 'a', being local to func() and on the stack, disappears.

Signature
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
attitudenine - 30 Nov 2006 19:44 GMT
Not a silly question at all.
Short answer is, Java does "Pass Reference by Value"
Check out
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
Cheers
Attitude Nine
---------------------------------------------------------------------------------
Built Exclusively using Attitude Version 9.0
http://www.opencontentonline.com
> hi
>
[quoted text clipped - 12 lines]
> return a;
> }
sakcee@gmail.com - 30 Nov 2006 23:22 GMT
Thanks all,
it was veyr helpful and helped me understand it a lot better.
thanks
> hi
>
[quoted text clipped - 12 lines]
> return a;
> }
warewolf - 04 Dec 2006 01:50 GMT
> hi
>
[quoted text clipped - 12 lines]
> return a;
> }
The way you declare a new Object in your function makes me think that
you come from a C/C++ background, and, if so, it is important to find a
new way of thinking about objects in Java. While the program is
running, the JRE maintains a large group of objects, which are all
accessed by reference. If you want a copy of an object, use the
clone() method, which makes sure that you are dealing with a different
object:
WalmartGreeter sam = new WalmartGreeter();
WalmartGreeter fred = sam;
/* at this point, fred == sam, and fred.equals(sam)
* ( the == operator determines if two variables are
* refering to the same individual object, but the
* equals() method only checks to see if the two
* objects are identical).
*/
fred = sam.clone();
/* at this point, fred != sam, but fred.equals(sam)
* because you have set fred to reference a clone of sam.
*/
fred = new WalmartGreeter();
/* here, you have re-assigned fred to be a totally new object,
* and, depending on how a new WalmartGreeter object is created,
* it may be that fred.equals(sam) (or not), but fred != sam.
* The clone of sam will be destroyed later automatically
* by the garbage collecter, since there is no reference to it anywhere
in the program
*/