Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static variable

 
santhoshkumar samala
Ranch Hand
Posts: 156
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
tim bond
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may be you are not right
 
vipul patel
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is a example of "shadowned variable"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic