Reputation: 1929
When the below code is executed
class Foo {
static int size = 7;
static void changeIt(int size) {
size = size + 200;
System.out.println("size in changeIt is " + size);
}
public static void main(String[] args) {
System.out.println("size = " + size);
changeIt(size);
System.out.println("size after changeIt is " + size);
}
}
the answer will be
size = 7
size in changeIt is 207
size after changeIt is 7
The answer which I expected was
size after changeIt is 207
Why the values of static variable is differing in the changeIt
method and the main
method ?
Upvotes: 2
Views: 167
Reputation: 75629
Java and many other languages do what is called local variable shadowing
. That means the local variable size
will "cover up" the class-level variable size
.
You can work around this by either using this.size
, or removing the size
parameter from your method signature.
If you want more details on Java specifically, you can consult the JLS 6.4.1.
Upvotes: 1
Reputation: 589
static void changeIt(int size) {
size = size + 200;
System.out.println("size in changeIt is " + size);
}
Assume that you replace "size" with "x". Then you can simply understand the answer you get is correct. Just like that "size" in the changeIt() method does not even compare with the static "size". Read bit more stuff about scope of variables.
Upvotes: 1
Reputation: 679
Because you use the parameter name 'size' which is same as the static variable. When you type
size = size + 1;
you change the value of local variable. Use
ClassName.size += 1;
instead.
Upvotes: 1
Reputation: 3173
Your method:
static void changeIt(int size)
{
size = size + 200;
System.out.println("size in changeIt is " + size);
}
Does not change your internal static member variable. It takes an integer and adds 200 to it. If you just want this method to add 200 to your static member variable your function should look like this:
static void changeIt()
{
size = size + 200;
System.out.println("size in changeIt is " + size);
}
To learn more about static variables, look here: http://en.wikipedia.org/wiki/Static_variable
Upvotes: 1
Reputation: 7457
change size = size + 200
to
Foo.size = size + 200;
size is local in your method, then you change the variable that you want.
The problem is that the parameter name size
is equal to name of the static field size
so when you point it in that method compiler considers the local variable.
Upvotes: 1
Reputation: 46239
Why the values of static variable is differing in the changeIt method and the main method ?
The values are different because you didn't change the static
variable size
, you changed the local variable size
. Change the code to:
static void changeIt() {
size = size + 200;
System.out.println("size in changeIt is " + size);
}
Or, if you want to use an argument with the same name, you can specify that you are using the static
class variable by qualifying it with the class name:
static void changeIt(int size) {
Foo.size = size;
System.out.println("size in changeIt is " + Foo.size);
}
Upvotes: 6