There's no need to pass x
as an argument to change
, and the fact that it's shadowing the member variable is the cause of the undesired behavior (your change
function is just modifying the argument instead of the member variable).
The following code is simpler and would have the desired effect:
public class Demorun
{
static int x = 20;
public static void change()
{
x = 10;
System.out.println("inside x: " + x);
}
public static void main(String[] args)
{
change();
System.out.println("outside x: " + x);
}
}