"Which variable(instance or local) is in action in the public method of foo class?"



似乎使用了该方法的局部变量!!!我以为它会在调用静态方法上的非静态变量时出错,但事实并非如此。

public class foo{
    int x = 12;
    public static void go(final int x){
        System.out.println(x);
    }
}

实际上它没有有趣的错误。

System.out.println(x)

将打印方法的参数去

如果要访问类的字段,请使用this键世界(如果您的方法不是静态的(:

System.out.println(this.x)

在您的情况下,您需要有一个 foo 类的实例并使用

foo f = new foo();
System.out.println(f.x);

相关内容

最新更新