• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

main calling which method?

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following code what will be the output?

1) 10030
2) 20030
3) 209930
4) 10020
Ans : 10020
I feel that the vaue of v.i is 20 and the value of i is 0.
but since Main is calling amethod()
v.i should be 30
a bit confused!!!
Sonir
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried to compile this? I get the following errors when I try:
java.lang.NoClassDefFoundError: ValHold
at ObParm.amethod(ValHold.java:15)
at ObParm.main(ValHold.java:10)
Exception in thread "main"

Are you sure you typed the code in correctly?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sonir shah:
Given the following code what will be the output?

1) 10030
2) 20030
3) 209930
4) 10020
Ans : 10020
I feel that the vaue of v.i is 20 and the value of i is 0.
but since Main is calling amethod()
v.i should be 30
a bit confused!!!
Sonir


You need to follow very carefully what is happening to the ValHold reference created with amethod. Let's go through the code as it's executed, line by line:
In the method amethod, i is set to 99 and we get a new ValHold object named v, so v.i is intially 10 (by ValHolder's constructor). The next line sets v.i to 30. Now we call another, passing it both i and v.
In another, the first thing that is done is that i is set to 0. This does nothing to the i from amethod because this is just a copy of that value (pass by value). The next thing we do is set v.i to 20. This does effect the v object from amethod because we were passed a copy of a reference to an object. Therefore, by modifying the object here, we're modifying the same object we were passed. The next line creates a new ValHolder object, vh, which has a vh.i value of 10. The next line sets v = vh. This is the tricky line and pay attention closely.
The v that is received by the method another is not the same v as is used in the method amethod. They are separate variables, even though they are named the same thing. The trick is that they both contain the same value at the beginning of another - a reference to the same object. That's why the line v.i = 20 in another had an effect on the object referenced by v in amethod. Now, however, we're assigning a new reference to the v is another - we're changing the contents of this v, but not the contents of the v in amethod. Did you catch that? That's the trick to the question. Printing out v.i and i give 10 and 0 respectively in this method.
Now, let's jump back to amethod. We're going to print out v.i, which is really 20. Remember, the first assignment in the method another affected the v object in this method, so that is reflected here.
Therefore, the answer is 10020.
I hope this all makes sense.
Corey
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic