File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static variable" Watch "static variable" New topic
Author

static variable

santhoshkumar samala
Ranch Hand

Joined: Nov 12, 2003
Posts: 156

class GFC404 {
private static int x=1;
static void m1(int x, int y) {x++; y++;}
public static void main (String[] args) {
int y=3; m1(x, y);
System.out.println(x + "," + y);
}}


What is the result of attempting to compile and run the program?

a. Prints: 1,3
b. Prints: 2,3
c. Prints: 1,4
d. Prints: 2,4
e. Run-time error
f. Compile-time error
g. None of the above


The answer given is a, my answer is b, since x is static variable it should be incremented in the method m1 and we should get the result 2,3

can we hide the static variable with local variable like above?


santhosh<br />SCJP,SCWCD
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Look carefully at the code one more time. Which x is being incremented?
It's the locally declared x in the parameter list.


Compile and run the code and see for yourself.
[ December 17, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
tim bond
Greenhorn

Joined: Nov 21, 2005
Posts: 18
/*What is the result of attempting to compile and run the program?

a. Prints: 1,3
b. Prints: 2,3
c. Prints: 1,4
d. Prints: 2,4
e. Run-time error
f. Compile-time error
g. None of the above


The answer given is a, my answer is b, since x is static variable it should be incremented in the method m1 and we should get the result 2,3

can we hide the static variable with local variable like above? */


hi the answer is a. 1,3 because there are 2 xs one is static x and other is local x of m .

when we call m(x,y)
and we are printing the values of static x and the variable y of the class.
since it is pass by value; the x and y in the m(x,y) get incremented to 2 and 4.
However the static x and the class variable y remain 1 and 3.
Balaji Sampath
Ranch Hand

Joined: Sep 30, 2005
Posts: 63
Hi there
Just a doubt. Is it not for the reason since the method m1 is void the values hat gets changed will not be effective for the variables in main method.. Please correct me if i am wrong..

regards
Balaji.S
xie li
Ranch Hand

Joined: Nov 30, 2005
Posts: 54
may be you are not right
vipul patel
Ranch Hand

Joined: Oct 16, 2005
Posts: 146
Guys, this is really brainer example.
I completely agree with Barry Gaunt for his suggestion. he is right.
Below are my comments:

1 private static int x=1;
2 static void m1(int x, int y) {x++; y++;}
3 public static void main (String[] args)
4 {
5 int y=3;
6 m1(x, y);
7 System.out.println(x + "," + y);
8 }

at line 6, compiler sees x is declared as static class level variable so he is happy with that and passes 1 to the method m1. now variable value 1 is copied to the m1 method's argument x.

"Method arguments are nothing but the method-local variables".

so now x is local to method m1 which is incremented. at line 7, method returns and at line 7 compiler prints x which is good-old static variable x with value 1.

Now try my version of code to really see as per your expectation.

1 private static int x=1;
2 static void m1(int z, int y) {x++; y++;}
3 public static void main (String[] args)
4 {
5 int y=3;
6 m1(x, y);
7 System.out.println(x + "," + y);
8 }

Here, line 7 will print 2,3.
Leonardo Rafaeli
Ranch Hand

Joined: Dec 12, 2005
Posts: 58
this is a example of "shadowned variable"


SCJP 1.4, SCWCD 1.4, SCBCD 5, OCUP, SCEA 5 (I), Oracle SOA Implementation Specialist, IBM Websphere Lombardi 7.1 Fundamentals
Blog: [url]http://www.leozin.com.br/blog[/url]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: static variable