【发布时间】:2012-07-14 13:25:33
【问题描述】:
可能重复:
What is the reason behind “non-static method cannot be referenced from a static context”?
Cannot make a static reference to the non-static method
cannot make a static reference to the non-static field
我无法理解我的代码有什么问题。
class Two {
public static void main(String[] args) {
int x = 0;
System.out.println("x = " + x);
x = fxn(x);
System.out.println("x = " + x);
}
int fxn(int y) {
y = 5;
return y;
}
}
线程“main”java.lang.Error 中的异常:未解决的编译问题: 无法从类型 Two 对非静态方法 fxn(int) 进行静态引用
【问题讨论】:
-
不是你的反对者,但你会想通过the Java tutorials 或一本基本的教科书来学习 Java 的基础知识。
-
如果您不了解我现在的情况,请不要对这个问题投反对票。 :/ 我还在 Head First Java 的第 4 章,对关于 return 的声明感到困惑。我只是想这样做。
-
在提出问题之前,您应该已经搜索了 SO 以获得答案。
-
@Stephen - 我搜索过。但它没有回答我的问题
-
@Kunal - 我有所不同。第二个“可能重复”与您的标题字符串非常匹配,并直接回答您的问题。如果你没有找到它,你看起来不正确。如果您确实找到了它,那么您没有正确阅读它。
标签: java