This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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 Java 8 in Action this week in the Java 8 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
 
Similar Threads
Referenec passing
compile time errors
about the static variable a
Doubt in calling the functions
Static int