• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

basic question about object passing

 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class IMP {
public static void main(String args[])
{
Test obj = new Test();
amethod(obj);
System.out.println(obj.meat);//NOTE
}
static void amethod(Test xobj)
{xobj.meat = "chicken";
xobj = null;
}
}
class Test {
String meat = "beef";
}
The line with the NOTE is something that i need clarifications.
when xobj object is set to null, then it basically
means that the object has been wiped out. right ? it that is so then the statement System.out.println(obj.meat); is ambigous. because there is no object obj now. BUT actually it prints out "chicken".
why ? Point is about the statement
xobj = null;
what is this actually doing. in my view it is just destroying the object. right ?
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For an excellent explaination of it, go to:Pass By Value story It gives an excellent explaination of it....
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Java employs a technique where all the reference are passed by value. Meaning a copy of that reference is passed.
A reference is a location in memory which holds the location of the object. Reference is not the same as the object. Neither Object is same as Reference. References are stored on the stack and Objects are created and stored in the Heap. Stack and Heap are places where Java organizes its memory. Stack uses the LIFO (Last in First Out) technique
We can go as technical as we want, but I don't wanna go off tangent with this topic.
That copy points to the same place where the original points too. Now this copy can change the internals of the object. This is reflected in the original copy. But if you make the copy point to a new object or null all you have done is modified the copy, the original is still intact. In other words all you have now done is the copy instead of pointing to the original it is now pointing to null which is nothing. It is pointing to nowhere.
I hope this helps!!!
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to Rick and Amish.
it is absolutely clear.
the key point is how the reference to a object "changes the internals of the actual object". and this actual object is only ONE. there can be any number of references to this actual object.
right ? if yes then let's end this thread. thanks again.

Originally posted by Amish A Patel:
Hi
Java employs a technique where all the reference are passed by value. Meaning a copy of that reference is passed.
A reference is a location in memory which holds the location of the object. Reference is not the same as the object. Neither Object is same as Reference. References are stored on the stack and Objects are created and stored in the Heap. Stack and Heap are places where Java organizes its memory. Stack uses the LIFO (Last in First Out) technique
We can go as technical as we want, but I don't wanna go off tangent with this topic.
That copy points to the same place where the original points too. Now this copy can change the internals of the object. This is reflected in the original copy. But if you make the copy point to a new object or null all you have done is modified the copy, the original is still intact. In other words all you have now done is the copy instead of pointing to the original it is now pointing to null which is nothing. It is pointing to nowhere.
I hope this helps!!!

 
Rick Salsa
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mark stone:

it is absolutely clear.
the key point is how the reference to a object "changes the internals of the actual object". and this actual object is only ONE. there can be any number of references to this actual object.
right ? if yes then let's end this thread. thanks again.



That is correct.
/rick
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class IMP {
public static void main(String args[])
{
Test obj = new Test();
System.out.println("outside has code before :"+obj.hashCode());
amethod(obj);
System.out.println("outside has code after :"+obj.hashCode());
System.out.println(obj.meat);//NOTE
}
static void amethod(Test xobj)
{xobj.meat = "chicken";
System.out.println("Inside has code :"+xobj.hashCode());
xobj = null;
}
}
class Test {
String meat = "beef";
}

This gives output :
outside has code before :7434986
Inside has code :7434986
outside has code after :7434986
chicken
So copy of refeence is send and restored after function call is returned.
reply
    Bookmark Topic Watch Topic
  • New Topic