File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Some confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Some confusion" Watch "Some confusion" New topic
Author

Some confusion

Vijay Kumar
Ranch Hand

Joined: Jul 24, 2003
Posts: 260
package com.test;

public class TestArgs {
private String str;
TestArgs(String str){
this.str=str;
}
public String getStr() {return str;}
public void setStr(String str) {this.str = str;}

public static void m1(TestArgs arg1,TestArgs arg2){
arg1.setStr("EARTH");
arg2=arg1;
}

public static void main(String[] args) {
TestArgs ar1=new TestArgs("SUN");
TestArgs ar2=new TestArgs("MOON");
m1(ar1,ar2);
System.out.println("ar1:"+ar1.getStr());
System.out.println("ar2:"+ar2.getStr());

}

}
OUTPUT :
ar1:EARTH
ar2:MOON
why only ar1 is reflecting not ar2 .In java objects are always passed by reference(passing the copy of reference variable).same hare I have passed the copy of reference ar1 & ar2.In method m1() arg2=arg1.It should reflect in main also.

Thanks
Teo Framoe
Greenhorn

Joined: Jan 05, 2007
Posts: 13
You are pointing m1's local arg2 to the arg1 object.
Main's arg2 remains unchanged.
Prahlad Joshi
Ranch Hand

Joined: Apr 21, 2007
Posts: 44
objects are passed by reference in the sense that the reference variable is copied so that you can never change the original reference variable but you can change the object(if it is not immutable) by accessing it through the copy of reference variable.
Sarah Jorden
Greenhorn

Joined: Sep 26, 2007
Posts: 16
Originally posted by Vijay Kumar:
package com.test;

public class TestArgs {
private String str;
TestArgs(String str){
this.str=str;
}
public String getStr() {return str;}
public void setStr(String str) {this.str = str;}

public static void m1(TestArgs arg1,TestArgs arg2){
arg1.setStr("EARTH");
arg2=arg1; // 1
}

public static void main(String[] args) {
TestArgs ar1=new TestArgs("SUN");
TestArgs ar2=new TestArgs("MOON");
m1(ar1,ar2);
System.out.println("ar1:"+ar1.getStr());
System.out.println("ar2:"+ar2.getStr()); // 2

}

}
OUTPUT :
ar1:EARTH
ar2:MOON




At 1 in above code, reference arg1's value has been copied to reference variable arg2. So, now arg2 is also pointing to the same object as arg1. But ar2 reference variable in main method is still pointing to the old ar2 object.

Now at 2 in your code, ar2.getStr() will still refer to the actual ar2 object.

insert one line to your code and you will see the difference. Replace m1 method in your code with method below,




I hope I'm not making it confusing.
Vijay Kumar
Ranch Hand

Joined: Jul 24, 2003
Posts: 260
hi all,
thanks for quick response Sarah I have already tested the changes before posted, which you have done.
Mr joshi answer is quit clear that during passing the object ,the reference variable is copied to the called method.So the original reference variable can never be modify.Although using the copied reference we can modify the object state.

Thanks all of you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Some confusion
 
Similar Threads
To compare 2 list and obtain the unmatched values from the list
Access to variables of enclosing class
How to pass value from one file to anaother
To compare 2 list and obtain the unmatched values from the list
Methods to access objects?