JAVA 不能静态引用非静态字段 [英] JAVA cannot make a static reference to non-static field

查看:43
本文介绍了JAVA 不能静态引用非静态字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在 JAVA 中的第一个程序,我无法理解此错误

this is my first program in JAVA and I'm having problem to understand this error

Cannot make a static reference to the non-static field *

不能对非静态方法进行静态引用*

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
");
      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;
   }

}

你能给我任何建议吗?我正在 Android 上的 SandIDE 上编码

Can you give me any suggest? I'm coding on SandIDE on Android

推荐答案

您正在从静态方法中调用实例方法和字段,这是无法完成的,因为没有对象,实例字段和方法不存在,并且在 main 方法内部没有 this 对象.您必须改为创建类的实例,然后调用实例上的方法.

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.

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
");

    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;
  }

}

许多其他问题,...

  • 您正在直接访问类字段,这是不应该做的.相反,字段应该是私有的,您应该使用 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.

编辑
你说:

我不明白相反,字段应该是私有的,你应该使用 getter/setter/contructor 参数来获取、设置和设置字段."我应该写 private float c_cfr() 吗?

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() ?

您的字段是:

float r;
float area;
float cfr;

这真的不是一个字段而是一个常量:最终双 pi = 3.14;

This is really not a field but a constant: final double pi = 3.14;

并且可以通过简单地使用 Math.PI 来替换/改进.

and can be replaced / improved by simply using Math.PI.

您的字段应更改为:

private float r;
private float area;
private float cfr;

你应该只通过公共的 getter 和 setter 方法访问它们,并且只有在绝对必要的情况下.

and you should only access them via public getter and setter methods, and only if absolutely necessary.

这篇关于JAVA 不能静态引用非静态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆