• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

passing Obj references......plz help.  RSS feed

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When we pass objects then the values changed in methods should change the state of object(bcoz the address of obj is passed).

According to my knowledge, output would be :
in first....
in second....
15 0
....second.
15
....first.
but it is not.
Please somebody explain it.
Thanks a lot.
M.Shahzad

All i know is that i know nothing.

 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
v.i=20;
changes the value of v.i, and in first() it is printed.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope this helps.
V1 I1=25
V2 I1=20
Val I2=15
V2 I2=15
At the end
V1 I1=20
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this. Hopefully, it'll help you visualize what's happening.
I hope that helps,
Corey
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that in the line I marked with stars you expect to change the object passed as parameter. This isn't so because the reference mechanism of Java acts on ObjectS, not in their references.
When you assign a new value to the parameter v in second(), the change is _local_ to the function (when the function returns the change is "undone"; the argument points again to the "old" Object). This is because, just like primitive types, references themselves are passed by value (even if it could be correct (and redundant) to say that the referenced objects are passed by reference).
However, if you assign a new value to a field in the object _referenced_ by v, the change is permanent. This is because the reference is reset, just like any other parameter, but not anything in the referenced object.
Your code (with the stars):
class tester {

public void first(){
System.out.println("In first....");
int i=15;
Value v=new Value();
v.i= 25;
second(v,i);
System.out.println(v.i);
System.out.println("....first.");

}
public void second(Value v,int i){
System.out.println("\tIn second....");
i=0;
v.i=20;
Value val=new Value();
v=val; // **********
System.out.println(v.i +" "+ i);
System.out.println("\t....second.");
}
public static void main(String args[]) {
tester obj=new tester();
obj.first();
}
}
 
Muhammad Shahzad
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks u guys for your kind help.
Welcome Mario Rossi....to JavaRanch. Your explanation is great.
Thanks all u guys.
Regards
M.Shahzad
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!