Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Some confusion

 
Vijay Kumar
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are pointing m1's local arg2 to the arg1 object.
Main's arg2 remains unchanged.
 
Prahlad Joshi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic