File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pass by value for static

 
eric lee
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since a is a static variable,why that answer is not 1?
public class X{
private static int a;
public static void main(String[] args){
modify(a);
System.out.println(a);
}
public static void modify(int a){
a++;
}
}
 
dragon ji
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a is a primitive type variable,and when it was invoked by method as a parameter,it passed by value,and in method modify,the variable a is a local variable,it only exist in the method scope.
correct me ,pls.
 
dragon ji
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the method modify as follows:
public static void modify()
{
a++;
}
then the output is 1,because a is a class variable.
 
eric lee
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe your right,dragon
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic