• Post Reply Bookmark Topic Watch Topic
  • New Topic

reference var  RSS feed

 
sheri
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen when you try compiling and running this code?
public class Ref{
public static void main(String argv[]){
Ref r = new Ref();
r.amethod(r);
}
public void amethod(Ref r){
int i=99;
multi(r);
System.out.println(i);
}
public void multi(Ref r){
r.i = r.i*2;
}
}

1) Error at compile time
2) An output of 99
3) An output of 198
4) An error at runtime
can anybody explain why this won't compile
thax
sheri
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sheri,
You can observe that there is no instance variable i associated with class Ref. In fact, in the code u gave i as a local variable for the method amethod(Ref r).
Hope this helps
-sampaths77
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!