[Solved] Variables in java [closed]


Your problem is you have declared a local variable in the second block, with the same name as the static variable.

So i in i += 2; in the second block is updating the i passed to the method, and not the static field.

So each call to m(i); will update i to 3, then do nothing with it. Confirm by adding System.out.println(i); to the method and they will both print 3.

If you wanted to update the static variable in the second method, you could use world.i += 2;.

Example

5

solved Variables in java [closed]