This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes call by reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "call by reference" Watch "call by reference" New topic

call by reference

mohammad shaid
Ranch Hand

Joined: May 05, 2010
Posts: 86

The output is 10020;;; I am confused with this output and call by reference... can anyone xplain ???

Thanks & Regards,
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

Hi Mohammed,
the first one prints "10"

This creates a new ValHold() and assigns to v; so v.i is 10.

second one prints local variable "i" and it is "0"

This method is trying to assign a new reference to the passed one. However it CANNOT change the original reference "v". But the "v.i = 20; " is still valid because the calling method can change the values of member variables (but not the reference). So the third one prints 20.

SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
rewati raman
Ranch Hand

Joined: May 22, 2010
Posts: 62
hi Shaid
lets start with main method first and lets see the flow of control.
ObParm o = new ObParm();
we know what happened above.
now in amethod
int i = 99; is creating a local variable of this method.
then v object of valhold is created
then v.i=30;
mean the i of v object has been changed to 30 as it is public. this dosnt mean the local variable of amethod is 30, that is still 99.
now another method is called. so the rest of code will follow only after the complete execution of another method.
in another method

i=0; is done which is of no use.
now the another method was passed an object v
code is changing v.i to 20 that mean before in amethod when it was changed to 30 is changed to 20 now.
another object of vhal is created that is vh
and vh is coppied to v.
v no more holds the previous object. now it is reffereing to new object . which was created recently.
so according to vhal the new object's i data member shud be 10
so when next print of v.i is executed it gives 10
then value of i
which was changed to 0
so we have now
now amethods last statment is executed that is v.i which was last changed to 20.
as v=vh wont be valid in this block. v was refrence in another
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

mohammad shaid wrote:I am confused with this output and call by reference... can anyone xplain ???

read this FAQ entry
mohammad shaid
Ranch Hand

Joined: May 05, 2010
Posts: 86
Thanks a lot Rajiv and Revati fo your efforts to explain.. i get the concept now.. Thanks a lot Friends..
mohammad shaid
Ranch Hand

Joined: May 05, 2010
Posts: 86
And thanks Seetharaman..
It is sorta covered in the JavaRanch Style Guide.
subject: call by reference
It's not a secret anymore!