这样的JAVA代码错了吗??

来源:百度知道 编辑:UC知道 时间:2024/05/29 09:27:10
public class MyClass {
int x = 10;
public static void main(String args[ ]){
for(int n = 3; n > 0; n--){
x = x * x;
}
System.out.println(x);
}
}

肯定是错的啦,在静态方法(main)中中是不能调用非静态的成员变量(x)的。只要把int x=10;改成static int x=10;就可以了。

错啦

x是非静态变量, 静态变量引用非静态变量, 你把int 前加个STATIC