Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes pass by value for static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "pass by value for static " Watch "pass by value for static " New topic
Author

pass by value for static

eric lee
Ranch Hand

Joined: Nov 04, 2002
Posts: 86
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

Joined: Oct 31, 2002
Posts: 110
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.


scjp 1.4<br />challenge haven't limit!
dragon ji
Ranch Hand

Joined: Oct 31, 2002
Posts: 110
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

Joined: Nov 04, 2002
Posts: 86
maybe your right,dragon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pass by value for static