This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes basic question about object passing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "basic question about object passing" Watch "basic question about object passing" New topic
Author

basic question about object passing

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
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 ?
Rick Salsa
Ranch Hand

Joined: Jul 17, 2001
Posts: 173
For an excellent explaination of it, go to:Pass By Value story It gives an excellent explaination of it....
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Dec 18, 2001
Posts: 417
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

Joined: Jul 17, 2001
Posts: 173
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
arun sahni
Ranch Hand

Joined: Jan 16, 2002
Posts: 91
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.


Arun Sahni<br />SCJP2,SWCD,<br />BEA Certified Specialist: Server <br />JCert Enterprise Developer for BEA.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: basic question about object passing
 
Similar Threads
Why? null is not a common object,then what it is?
q from jiris.com
from mock exam qs..Jquest
Difference between Null String and Null Object
string initialization