JAVA无法对非静态字段进行静态引用 [英] JAVA cannot make a static reference to non-static field

查看:321
本文介绍了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\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;
   }

}


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

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

推荐答案

您正在静态方法中调用实例方法和字段,由于实例字段和方法在没有对象的情况下不存在,并且在main方法内部没有这个对象,所以无法完成。您必须改为创建类的实例,然后调用实例上的方法。

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\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,...


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

编辑

你说:

Edit
You state:


我不明白相反,字段应该是私有的,你应该使用getters / setters /用于获取,设置和设置字段的构造函数参数。我应该写私人浮动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;

这不是一个字段而是一个常数:
final double 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天全站免登陆