aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes main calling which method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "main calling which method?" Watch "main calling which method?" New topic
Author

main calling which method?

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
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
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
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?


Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="http://alia.iwarp.com" target="_blank" rel="nofollow">http://alia.iwarp.com</a>
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: main calling which method?