• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Completely stumped at this

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic