File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes which values will be printed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "which values will be printed?" Watch "which values will be printed?" New topic
Author

which values will be printed?

maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
[/code]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

did you run ?

P.S: Good Example to pass by value of reference and primitive

Purvesh Maheshwari
Greenhorn

Joined: Dec 30, 2009
Posts: 2
Ans will be 4) 10020
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
answer is 4. 10020


SCJP 1.6 96%
SanjayR Jain
Ranch Hand

Joined: Mar 05, 2009
Posts: 43
good question answer will 10020
Kamil Wojcik
Ranch Hand

Joined: May 19, 2008
Posts: 78

We know that the answer will be 10020, but can someone explain, why at the and we get 20? Thanks


SCJP 6.0 Passed | SCWCD Passed | PSM in progress...
Shams Tabrez Ahmed
Greenhorn

Joined: Nov 27, 2008
Posts: 2
code at line 20 changes the value of int i of the object instantiated at line 12
Ben Power
Ranch Hand

Joined: Dec 30, 2009
Posts: 31
Kamil Wojcik wrote:We know that the answer will be 10020, but can someone explain, why at the and we get 20? Thanks


At line 20, the another() method changes v.i to 20. Remember, when you pass an Object reference to a method, you pass a copy of the reference, not a separate copy of the object. So now, v.i points to the same v that was declared in line 12. So when the another() method completes and we resume at line 15, the value of v.i has been changed to 20. Does that make sense?

This is an excellent question.

Ben


SCJP 6
Rajiv Chopra
Ranch Hand

Joined: Dec 19, 2008
Posts: 62
Hey when you pass a referance variable to a method you are not only passing the address of that referance variable but that is copy of that referance variable address.
This means that you are passing copy of referance variable to ValHold v.
Then you are changing that value to vh(another object) The previous referace object is still same uneffected.

Remember you can always change the value of the passing object but you can't change the passing object to refer to other object.


Rajeev Kumar
SCJP 6.0 Certified 96%
Rajiv Chopra
Ranch Hand

Joined: Dec 19, 2008
Posts: 62
Hey when you pass a referance variable to a method you are not only passing the address of that referance variable but that is copy of that referance variable address.
This means that you are passing copy of referance variable to ValHold v.
Then you are changing that value to vh(another object) The previous referace object is still same uneffected.

Remember you can always change the value of the passing object but you can't change the passing object to refer to other object.

Answer is abc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: which values will be printed?
 
Similar Threads
call by reference
weird call by ref question
Can someone explain!!!
instance variable
Question on References