• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Marcus Green - Question 51

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
I would like to get help on the following
Question 51)
Given the following code what will be the output?
class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.println(v.i);
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.println(v.i+ " "+i);
}//End of another
}
1) 10,0, 30
2) 20,0,30
3) 20,99,30
4) 10,0,20
Answer
Objective 5.4)
4) 10,0,20
In the call
another(v,i);
A reference to v is passed and thus any changes will be intact after this call.
1. In method another v is assigned by vh a new instance of ValHold. So v.i will be 10.
2. i is a local variabl in method another. So its value will be 0 ( as initialized in method another ).
3. Here is my doubt. Why does the System.out.println(v.i); in amethod print 20 ? If the reference has been assigned by vh in method another here also this should be 10 ??? Really confused. Help please !!!
Thanks
ARS Kumar
 
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the another method v.i gets assigned the value 20. Right after this another ValHold is created and then the reference v is assigned to this new object. This is how you get the 10 in the printout.
Remeber that you are dealing with the reference when you say
v = vh;
once you return from another method v is pointing to the original object and in the another method you had assigned v.i = 20;
So you get a printout of 10,0,20.
So, manipulation of the copy of the reference passed in to a method, like v=vh, does not affect the reference outside the method scope. Changes to the actual object, like v.i = 20, permanently affect the object since objects don't have scope.
John
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kumar,
1. public void amethod(){
2. int i = 99;
3. ValHold v = new ValHold();
4. v.i=30;
5. another(v,i);
6. System.out.println(v.i); }//End of amethod
7. public void another(ValHold v, int i){
8. i=0;
9. v.i = 20;
10. ValHold vh = new ValHold();
11. v = vh;
12. System.out.println(v.i+ " "+i);
13. }//End of another
Some Basics:
When an argument is passed into a method call, it is actually a copy of the method argument that gets passed. So when an argument is passed into a method, changes to argument are not visible outside the method i.e. it does not affect the original data.

Let us look at our present example. ValHold and i are the 2 args passed to method another. On line 8, the value of v.i is set to 20. On the following line, vh is assigned to v thus setting the value of v.i to 10. Note that value of i is already changed changed to 0 on line 8. Thus when line 12 is executed we get 10 and 0 as the output. After this control is passed to the line 6. Now as per our rule the changes to method argument are not visible outside method another. So it will display 20 as the output.
Hope this helps!!
Regards,
Milind
[This message has been edited by Milind (edited May 29, 2000).]
[This message has been edited by Milind (edited May 29, 2000).]
 
ARS Kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
I guess I am close to you guys. But still little more...
Let us take a look at method another again

public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.println(v.i+ " "+i);
}//End of another

Here i is a local variable to method another. So in System.out.println(v.i+ " "+i); we are trying to print the local variable i whose value is set to 0. Clear about this !
So from the explanation's of John and Milind, I think I should consider these two lines of code differently.
1. v.i = 20;
2. v = vh;
v.i is changing the object references data directly. So value of v.i will set to 20. v = vh means v has been assigned to the new ValHold reference vh. So the value of v.i is 10.
Is this object reference vs object's data ? I guess I am OK with this now.
Thanks a lot John and Milind for helping me on such basic things !
ARS Kumar.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am somewhat confused ..why System.out.println(v.i);
is not printing v.i=30 of amethod() instead of v.i=20 of another()..what about scope here...
if any one can help..
Wali
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
System.out.println(v.i);
another(v,i);
System.out.println(v.i+" "+i);
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.println(v.i+ " "+i);
}//End of another
}

I have added an extra line and made some modification in the another highlighted line just to reinforce the fact that changes to the argument value by the method are not reflected outside the method.
On running the changed program I got the following output:
30
10 0
20 99
Value of v.i before the first method call is 30. Value of i ( one of the args.) is changed to 0 in the method another. But this change is not visible outside the method.
Also since v.i does not belong to method another (it's not automatic) so changes applied in method another are visible outside it.
Hope this helps!!
Regards,
Milind

[This message has been edited by Milind (edited May 29, 2000).]
[This message has been edited by Milind (edited May 29, 2000).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic