This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes passing Obj reference to the method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "passing Obj reference to the method" Watch "passing Obj reference to the method" New topic
Author

passing Obj reference to the method

chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Sep 15, 2006
Posts: 134
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

Joined: Feb 28, 2007
Posts: 1707
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


cmbhatt
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by chintan ramavat:
...anybody can make it more clear ? in any way verbose or technically...

Try this Pass-by-Value... article.
 
Consider Paul's rocket mass heater.
 
subject: passing Obj reference to the method
 
Similar Threads
Argument-Defined Anonymous Inner Class
Passing variable into methods
Pass-by-Vaule Object reference question
Passing Object Reference Variables
Passing by value...