This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About an Object reference being passed to a method call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About an Object reference being passed to a method call" Watch "About an Object reference being passed to a method call" New topic
Author

About an Object reference being passed to a method call

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Sir,
When an object is sent to a method call - a reference is passed. And hence if this object is affected, the result after the method call in the calling environment will be the value thus changed. Hence must this not be the original reference???
In the following program why obj1 does not change value?
A program
import java.util.Stack;
class e1 extends Stack
{
static public void main(String[]o)
{
new e1();
}
e1()
{
Stack obj1 = new Stack();
Stack obj2 = new Stack();
System.out.println("before \n");
System.out.println("obj1="+obj1);
System.out.println("obj2="+obj2);
change(obj1,obj2);
System.out.println("after \n");
System.out.println("obj1="+obj1);
System.out.println("obj2="+obj2);
}
static void change(Stack s1, Stack s2)
{
System.out.println("in");
s1.push(new Integer("100");
System.out.println("\n push occured");
System.out.println("s1="+s1);
System.out.println("s2="+s2);
s2=s1;
System.out.println("\n s2 re-initialized");
System.out.println("s1="+s1);
System.out.println("s2="+s1);
}
}
Kindly give me your comments if I am wrong or agree this is an error in the roundup

[This message has been edited by Lionel (edited May 16, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
please change the code in bold
System.out.println("\n push occured");
System.out.println("s1="+s1);
System.out.println("s2="+s1);
to
System.out.println("s2=" +s2);
the code works as per expectation. if u have any doubt please write again as to where exactly u r having a problem i mean which line.
[This message has been edited by rahul_mkar (edited May 16, 2000).]
[This message has been edited by rahul_mkar (edited May 16, 2000).]
[This message has been edited by rahul_mkar (edited May 16, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
When the method change is called what is passed the original reference or a copy? If its a copy why does it change the value of obj1? and not obj2?
[This message has been edited by Lionel (edited May 16, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi
hope this helps
concentrate on the addresses of reference and that of object and remember copy of reference is passed not the reference itself
& means address of the reference
* means address of the object to which it points
e1()
{
Stack obj1 = new Stack();
//obj1=&100 *500 means address of obj1 reference is 100 and is pointing to an object stored at address 500
Stack obj2 = new Stack();
// obj2=&200 *600
System.out.println("before \n");
System.out.println("obj1="+obj1);
System.out.println("obj2="+obj2);
change(obj1,obj2);

//obj1 still &100 *500
///obj2 still &200 and address 600

System.out.println("after \n");
System.out.println("obj1="+obj1);
System.out.println("obj2="+obj2);
}
static void change(Stack s1, Stack s2)
//s1=&300 *500 s2=&400 *600
//note address of obj1!=s1 obj2!=s2
// thus a copy is passed
{
System.out.println("in");
s1.push(new Integer("100");
//change of object has occured
System.out.println("\n push occured");
System.out.println("s1="+s1);
System.out.println("s2="+s2);
s2=s1;
//object at *600 has not changed reference has changed to another object
//s2=&400 *500
//note the object it is pointing to has changed to 500
System.out.println("\n s2 re-initialized");
System.out.println("s1="+s1);
System.out.println("s2="+s1);
}
//the references s2 and s1 are no longer in memory

[This message has been edited by rahul_mkar (edited May 16, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks man. The address and the value given as comments for each line of code was helpful
[This message has been edited by Lionel (edited June 03, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About an Object reference being passed to a method call