Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

greenhorn needs help........

 
Rowan Chattaway
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm just going through the marcus green mock 2:
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.print( v.i );
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh; //LineX
System.out.print(v.i);
System.out.print(i);
}//End of another
}
1) 10030
2) 20030
3) 209930
4) 10020
The answer is 4)
can someone explain this to me please?.
I thought the assignment "v = vh;" at //LineX would mean v.i would equal 10
so:
10010
?
Any help would be much appreciated!
Rowan
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
 
Junilu Lacar
Bartender
Posts: 7482
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this thread out and follow the link to Corey's cool Flash illustrating how parameters are passed around.
Junilu
[ March 07, 2002: Message edited by: Junilu Lacar ]
 
Rowan Chattaway
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I should have searched previous threads for similar questions.
Cheers, I understand that now!
Rowan.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic