This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
1) 10030 2) 20030 3) 209930 4) 10020 Ans is 4,in my opinion,I think the answer should be 10010,because v = vh, so the System.out.print( v.i ) should be 10,so what's the problem ? [ Jess added UBB [code] tags to preserve whitespace -- check 'em out! ] [ July 13, 2002: Message edited by: Jessica Sant ]
Ok, the thing to remember here is that all variables are passed by value and not by reference. However, when the variable is a reference to an object, then any changes made to the object through that reference will be apparent to other methods. in the class another, you first change the variable i to 20 through the reference v. then you create a new instance of calss ValHold and store the reference to vh which has a new copy of the variable i which has a value of 10. Ofcourse when you change the variable i through the reference v, the change is apparent to the amethod method. HTH [ July 13, 2002: Message edited by: Amir Ghahrai ]
At 0 you create a reference var and point it to an object created by new operator in memory The variable v at 2 is a local member of the meth another(), when u call this method you are passing out object (at 1) of the ValHold that you created at 0.This means that v refers to an object that lies in memory created at 0.
Now at 3 when you set i=20, you are changing the value of object reference ,laying in memory, and the one referred (pointed) by v at 0 so at 3 there is a global change to the reference created by new operator at 0 At 4 you set the meth another()'s local variable v to refer to a new object created by new operator, which has i=10 . When you come out of another() the local var v fails to exist and therefore the new reference that it had no is longer pointed by any reference( a perfect member for gc()) But inside amethod() v still holds the reference of object created at 0 and whoose member was altered at 3. Semantics remains the same as mentioned by Amir.. Thanks Bindesh Vijayan