File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt from the question!

 
fengqiao cao
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, there
the output of following code is 10 and 40(i compiled and ran). i know why 10 got printed out, but i don't know why 40 got printed out(i thougt 20 would be the output). could some one explain to me??
public class Pass{
static int j=20;
public static void main(String argv[]){
int i=10;
Pass p = new Pass();
p.amethod(i);
System.out.println(i);
System.out.println(j);
}

public void amethod(int x){
x=x*2;
j=j*2;
}
}
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
j is 20
2 * 20 is 40
Note that j has a class scope while the scope of i is within the method main !
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Junilu Lacar
Bartender
Pie
Posts: 6548
22
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It prints out 40 because the j referenced in amethod() is the j declared as static int in the class, so whatever you do to it in amethod() will be visible in main(). i is not affected, of course, because x is local to amethod() and any changes to it are not visible in main().


------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
 
fengqiao cao
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
thanks both of you! i got it.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic