Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing Obj reference to the method

 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends

i have question regarding passing Object Reference to the method, can anybody explain it please in verbose or by example,

void bar() {
Foo f = new Foo();
dostuff(f);
}
void dostuff(Foo g) {
g = new Foo();
}

" dostuff() method does not have a way to get to the f reference variable, so dostuff() method can chnge what f refers to, but can't change the actual contents of it. " thats an excerpt from KB. I could not understand technically.

thanks

chintan
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chintan ramavat:
..."dostuff() method does not have a way to get to the f reference variable, so dostuff() method can chnge what f refers to, but can't change the actual contents of it. " thats an excerpt from KB...

Isn't this the other way around?

doStuff() can't change what f refers to, but can change the actual contents of it.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chintan ramavat:
hello friends

i have question regarding passing Object Reference to the method, can anybody explain it please in verbose or by example,

void bar() {
Foo f = new Foo();
dostuff(f);
}
void dostuff(Foo g) {
g = new Foo();
}

" dostuff() method does not have a way to get to the f reference variable, so dostuff() method can chnge what f refers to, but can't change the actual contents of it. " thats an excerpt from KB. I could not understand technically.

thanks

chintan


Actually, that's backwards.

Within the method, as long as g has not been assigned to point to a new object, you can change the contents of the object referred to by f (assuming the object is mutable).

However, within the method you cannot change what f points to.

The reason is that all parameter passing in Java is by value.

When the parameter is an object reference, then a copy of the reference is sent to the method.

So when you assign the parameter of the method to point to another object, it doesn't affect where the actual parameter to the call is pointing.
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank mr.Weber and Lynn

Here it drives my crazy about pass-by-value n by reference.

what i understood is " we cannot change OBJECT variables meaning its not pass by reference while we can change object-reference's variable thats why its pass by value "

i am pretty much sure that there's nothing like pass by reference in java

anybody can make it more clear ? in any way verbose or technically.

thanks a ton in advance

chintan
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chintan,

IMHO you should forget the term pass by value and pass by reference first.
See how simply you can understand the duo:

If you create another instance and assign it to the reference variable passed in the method it will have local impact.

If you change the content of the object refered by the reference variable passed in the method, it would have global impact.

Or, in the way examples are used to convey the concept:

Output:
Before passing p1 to doStuff1() : Jackal
After passing p1 to doStuff1() : Jackal
After passing p1 to doStuff2() : Baytaal


You can see the output and think what I discussed above. You should be in good shape now!


Thanks and Regards,
cmbhatt
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chintan ramavat:
...anybody can make it more clear ? in any way verbose or technically...

Try this Pass-by-Value... article.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic