JAVA 不能静态引用非静态字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19482739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAVA cannot make a static reference to non-static field
提问by Mitro
this is my first program in JAVA and I'm having problem to understand this error
这是我在 JAVA 中的第一个程序,我无法理解此错误
Cannot make a static reference to the non-static field *
and
和
Cannot make a static reference to the non-static method *
不能对非静态方法进行静态引用 *
public class Cerchio{
float r;
float area;
float cfr;
final double pi = 3.14;
public static void main(String[] args){
System.out.println("CIRCLE PROGRAM\n");
r = 5;
c_cfr();
c_area();
System.out.ptintln("The cir is: " + cfr);
System.out.println("The area is: " + area);
}
float c_cfr(){
cfr =(float)(2 * pi * r); //casting
return cfr;
}
float c_area(){
area = (float)(pi * (r*r));
return area;
}
}
Can you give me any suggest?
I'm coding on SandIDE on Android
你能给我任何建议吗?我在 Android 上的 SandIDE 上编码
采纳答案by Hovercraft Full Of Eels
You are calling instance methods and fields from within a static method, something that can't be done because instance fields and methods don't exist without an object, and inside of the main method there is not this
object. You must instead create an instance of the class, and then call the methods on the instance.
您正在从静态方法中调用实例方法和字段,这是无法完成的,因为没有对象就不存在实例字段和方法,并且在主方法内部没有this
对象。您必须改为创建类的实例,然后调用实例上的方法。
public class Cerchio{
float r;
float area;
float cfr;
final double pi = 3.14;
public static void main(String[] args){
System.out.println("CIRCLE PROGRAM\n");
Cerchio cerchio = new Cerchio();
cerchio.r = 5;
cerchio.c_cfr();
cerchio.c_area();
System.out.ptintln("The cir is: " + cerchio.cfr);
System.out.println("The area is: " + cerchio.area);
}
float c_cfr(){
cfr =(float)(2 * pi * r); //casting
return cfr;
}
float c_area(){
area = (float)(pi * (r*r));
return area;
}
}
Lots of other problems,...
还有很多问题,...
- You're accessing class fields directly, something that shouldn't be done. Instead, the fields should be private and you should use getters/setters/contructor parameters to get, set and set the fields.
- Your code is unindented making it very hard to read and understand.
- 您正在直接访问类字段,这是不应该做的。相反,字段应该是私有的,您应该使用 getter/setter/contructor 参数来获取、设置和设置字段。
- 您的代码未缩进,因此很难阅读和理解。
Please search this site as this same question has been asked and answered a gabizillion times, and most likely there's an answer out there that is much better than mine. If found, then this question should be closed as a duplicate.
请搜索这个网站,因为同样的问题已经被问过并回答了 gabizillion 次,而且很可能有一个比我的好得多的答案。如果找到,则此问题应作为重复项关闭。
Edit
You state:
编辑
你说:
I didn't understand "Instead, the fields should be private and you should use getters/setters/contructor parameters to get, set and set the fields." I should write private float c_cfr() ?
我不明白“相反,字段应该是私有的,你应该使用 getter/setter/contructor 参数来获取、设置和设置字段。” 我应该写 private float c_cfr() 吗?
Your fields are:
您的字段是:
float r;
float area;
float cfr;
This is really not a field but a constant: final double pi = 3.14;
这真的不是一个字段而是一个常量:final double pi = 3.14;
and can be replaced / improved by simply using Math.PI.
并且可以通过简单地使用 Math.PI 来替换/改进。
Your fields should be changed to:
您的字段应更改为:
private float r;
private float area;
private float cfr;
and you should only access them via public getter and setter methods, and only if absolutely necessary.
并且您应该只通过公共 getter 和 setter 方法访问它们,并且仅在绝对必要时。
回答by David Allan Houser Jr
The simple fix is to put the word static in front of each method. It is a universal static truth circumference =2pi*r your circle may be bigger than my circle (both instances of circle) but to find the area there is one formula
简单的解决方法是在每个方法前面加上 static 一词。这是一个普遍的静态真理 周长 =2pi*r 你的圆可能比我的圆(圆的两个实例)大,但要找到面积有一个公式
回答by Vivek Vermani
c_cfr() and c_area() are non static methods which you are trying to call directly from the static main method. Either make the methods c_cfr() and c_area() as static too or access them using object reference.
c_cfr() 和 c_area() 是非静态方法,您试图直接从静态 main 方法调用它们。将方法 c_cfr() 和 c_area() 也设为静态,或者使用对象引用访问它们。