File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Completely stumped at this 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 » Java » Beginning Java
Bookmark "Completely stumped at this" Watch "Completely stumped at this" New topic
Author

Completely stumped at this

Parag Kale
Greenhorn

Joined: Apr 08, 2001
Posts: 25
Hi!
I found this on one of the mock exams.
After the following code is run, the output is 10,0,20 and that is also supposed the be the right answer. But i think the answer should be 10, 0, 30. Can somebody explain what really happens to the value of v.i, in detail ?

Thanx,
Parag
Rebecca Pickett
Greenhorn

Joined: Aug 16, 2000
Posts: 15
Originally posted by Parag Kale:
Hi!
I found this on one of the mock exams.
After the following code is run, the output is 10,0,20 and that is also supposed the be the right answer. But i think the answer should be 10, 0, 30. Can somebody explain what really happens to the value of v.i, in detail ?


Parag,
You have to walk through it carefully you'll get tripped up. I think here's what's confusing you: When the method amethod() calls the another() method, it is sending a reference to the v (ValHold) object. The first thing another() does is reset i to 20. It is resetting the original object's value since it only passed a reference to the object, not the object itself nor a copy of the object. If you read the Campfire's "Cup Size" essay, you'll see my point. Following that logic through, you'll see that i is really 20 when it is finally printed out at the end of amethod().
[This message has been edited by Rebecca Pickett (edited April 10, 2001).]
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
cleaned up a bit:

Note that over in another(), you're monkeying with a copy of v, even though
it refers to the same object that the original v refers to. when you get back
to aMethod(), you've still got that original v waitin' there still referring
to the original ValHold object.
Umm.. also note the coding conventions in the chicken coop to make your
code look spiffy like above!
Parag Kale
Greenhorn

Joined: Apr 08, 2001
Posts: 25
OK...i got it now.I needed to think THAT way.
About the code part, i'll remember that. Promise clean code the next time.
Thanx a bunch.
Cheers,
Parag
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Completely stumped at this
 
Similar Threads
Marcus Green sample question
how to explain the following question?
Marcusgreen Mock Exam1 Question 51
urgent pls explain
urgent pls explain