text: *
  A friendly place for programming greenhorns!
  Big Moose Saloon
  Search
  |
  Java FAQ
  |
  Recent Topics
  |
  Flagged Topics
  |
  Hot Topics
  |
  Zero Replies
  Register / Login
  JavaRanch
  »
  Java Forums
  »
  Certification
  »
  Programmer Certification (SCJP/OCPJP)
  Author
  Question about Mock Exam 2
  kamesh vadarevu
  Greenhorn
  Joined: Oct 17, 2000
  Posts: 20
  posted
  Oct 20, 2000 18:06:00
  0
  Hi,
  Can anyone please help me in clearing the doubt I have with the Object reference in the following program.The output of this is 10 0 20.But when I walk through the program I am getting 10 0 10.the reason I think is after the statement v=vh;v is pointing to the object with the value of i being 10.In that case the final output will be 10 0 10 why am I getting 10 0 20.I am confused can some one help me in this regard.thanks in advance.
  kamesh.
  here is the code from Marcus Mock exam }
  I added UBB code here - Mapraputa
  [This message has been edited by Mapraputa Is (edited October 20, 2000).]
  Udayan Naik
  Ranch Hand
  Joined: Oct 18, 2000
  Posts: 135
  posted
  Oct 20, 2000 23:49:00
  0
  Hi Mr Kamesh.Let's walk through the program once again.
  1)
  ValHold v = new ValHold();
  v.i=30;
  another(v,i);
  This code creates a obj. reference v and passes that reference to the method another().Inside another we have a LOCAL VARIABLE v.This variable also points to the same obj. v created in method amethod().So now we have TWO REFERENCES pointing to same obj.
  2)Inside method another , v.i=20; is execeuted.This means that value of instance variable i of obj. v is changed from 30 to 20.
  3)Now a new obj. is created referenced by LOCAL VARIABLE vh.The lOCAL VARIABLE v is assigned this value.Thus LOCAL VAR. v is detached from the original object it was pointing to and now LOCAL VAR. v and vh point to same object.Since vh is a new obj. instance var vh.i = 10 and HENCE v.i=10.
  LOCAL VARIABLE i is assigned value 0 on entry to method another() so printing these values gives 10 and 0 respectively.
  3)The original object ref v is unchanged , it is still pointing to the object created in aMethod().Instance variable. i value was changed to 20 in another(),so this prints 20.
  Thus final answer is 10 0 20.
  I hope i have been clear enough.
  ------------------
  Come on in !! Drinks are on the house in the Big Moose Saloon !!
  Udayan Naik<BR>Sun Certified Programmer for the Java 2 Platform
  Gautam Pandya
  Greenhorn
  Joined: Sep 29, 2000
  Posts: 17
  posted
  Oct 20, 2000 23:58:00
  0
  When the 'another' method is called from the method 'amethod' with passing the variable 'v', only the copy of the value stored in that variable (which is a reference to an object) is passed. Which means after the method another is called it will have a variable 'v'
  different from
  the variable 'v' of the calling method (amethod) but having the
  same value
  as that of the variable 'v' of the calling method. Which means both variables hold references which points to the same object.
  So v.i = 20 statement in another affects the original object because v in both methods (before v=vh) refer only one object.
  Now in another method, after the statement v=vh, v refer a new object but it does not change the value of v in the calling method thus v in the calling method still holds the original reference and not a new reference to the object created in the method - another.
  kamesh vadarevu
  Greenhorn
  Joined: Oct 17, 2000
  Posts: 20
  posted
  Oct 21, 2000 11:38:00
  0
  Thanks Uday and Gautam.But I am still confused when we are able to change the value in the another() from v.i=30 to v.i=20 what i could understand is that the objects are passed by reference.Uday when you say that local variable of another() v is also pointing to the same object ie the object created in amethod() I am not able to get that (can you please tell me which line of the code shows that)Actually I am getting confused.I think my Funda is going wrong.Can anyone suggest a book which clears the passing by refernece or passing by copy of the reference(as used by Gautam).
  I would appreciate for the help.I know these are the basic things but still i am not confident.
  kamesh
  Jane Griscti
  Ranch Hand
  Joined: Aug 30, 2000
  Posts: 3141
  posted
  Oct 21, 2000 12:56:00
  0
  Hi Kamesh,
  I've added comments to your example to explain what's happening.
  class ValHold{ public int i = 10; } public class ObParm{ public static void main(String argv[]){ ObParm o = new ObParm(); o.amethod(); } public void amethod(){ int i = 99; // local variable ValHold v = new ValHold(); // v = object reference v.i=30; // sets value i in ValHold object another(v,i); // passes a copy of the object reference v // and a copy of the local variable i System.out.println(v.i); // prints the value from the original object // reference which was modified in 'another' // to 20 System.out.println(i); // prints original value of local variable i // it is a copy that gets changed in // 'another' }//End of amethod public void another(ValHold v, int i){ i=0; // sets the copy of i v.i = 20; // sets the object value of i (remember // v is a copy of the reference to the // object; still points to the original // object ValHold vh = new ValHold(); // creates a new object and object // reference v = vh; // sets the object reference to the new // object pointed to by 'vh' System.out.println(v.i+ " "+i); // prints the value 'i' from the new object // (pointed to by vh - 10) and the value // of the local variable i - 0 }//End of another }
  The thing to remember is that Java passes everything as a copy of the original
  value
  . If you pass a primitive type; a copy of that type and value is created and given to the method. If you pass an object reference, a copy of the object reference is given to the method.
  A copy of an object reference still points to the original object. Which is why
  v.i = 20
  changes the value in the ValHold object used by
  aMethod
  . When you created
  vh
  you created a new ValHold object. Assigning
  vh
  to
  v
  changed the
  value of the object reference
  in the method
  another
  BUT has no effect on the original object reference created in
  aMethod
  .
  Hope that helps.
  ------------------
  Jane
  Jane Griscti
  SCJP, Co-author
  Mike Meyers' Java 2 Certification Passport
  kamesh vadarevu
  Greenhorn
  Joined: Oct 17, 2000
  Posts: 20
  posted
  Oct 21, 2000 14:50:00
  0
  Jane,
  Thanks a lot and also thanks once again to UDay and Gautam.Can someone suggest me a site where I can practice this type of problems so that i my self become familiar with passing parameters by value and reference.Thanks in advance.
  kamesh.
  I agree. Here's the link:
  subject: Question about Mock Exam 2
  Similar Threads
  Marcus Green sample question
  A funny Mock Question! Please help me to solve.
  Question on References
  Marcusgreen Mock Exam1 Question 51
  passing reference, very confused, pls help
  All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
  JForum
  |
  Paul Wheaton