• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Some confusion

 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are pointing m1's local arg2 to the arg1 object.
Main's arg2 remains unchanged.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic