jQuery in Action, 2nd edition*
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 Android Security Essentials Live Lessons this week in the Android 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
 
jQuery in Action, 2nd edition
 
subject: pass by value for static
 
Similar Threads
Static int
about the static variable a
Doubt in calling the functions
compile time errors
Referenec passing