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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21839182/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:00:45  来源:igfitidea点击:

cannot make a static reference to a non static method

javastatic-methods

提问by user3221816

So far I have the following code:

到目前为止,我有以下代码:

import java.util.Scanner;

public class HallLanceMemoryCalculator {
private double currentValue;

public static int displayMenu(){

    Scanner input=new Scanner(System.in);

    int choice=0;

    while(choice<1||choice>5){      
    System.out.println("1.Add");
    System.out.println("2.Subtract");
    System.out.println("3.Multiply");
    System.out.println("4.Divide");
    System.out.println("5.Clear");

    System.out.println("What would you like to do?");
    choice=input.nextInt();
    }
    return choice;
}

public static double getOperand(String prompt){
    Scanner input=new Scanner(System.in);
    System.out.println("What is the second number?");
    double secondNumber=input.nextDouble();
    return secondNumber;
}

public  double getCurrentValue(){
    return currentValue;
}

public void add(double operand2){
    currentValue+=operand2;
}

public void subtract(double operand2){
    currentValue-=operand2;
}

public void multiply(double operand2){
    currentValue*=operand2;
}

public void divide(double operand2){
    currentValue/=operand2;
}

public void clear(){
    currentValue=0;
}

public static void main(String[] args) {
    double value=getCurrentValue(); 
}

}

}

When I try to set double value=getCurrentValue();at the end, I get an error message "Cannot make a static reference to the non-static method." It says the fix is to make the getCurrentValue()method static as well, but I was told not to make that field static by my professor. Is there a simple solution to this that I am just missing?

当我尝试value=getCurrentValue();在最后设置 double时,收到错误消息“无法对非静态方法进行静态引用。” 它说修复getCurrentValue()方法也是使方法静态化,但我的教授告诉我不要使该字段静态化。有没有我只是想念的简单解决方案?

采纳答案by trutheality

A static method belongs to the class, a non-static method belongs to an instanceof the class.

静态方法属于类,非静态方法属于类的实例

When you call getCurrentValue()from main, you get an error because mainisn't associated with any instance.

当您调用getCurrentValue()from 时main,您会收到一个错误,因为main它不与任何实例相关联。

You need to create an instance of the class:

你需要创建一个类的实例:

HallLanceMemoryCalculator me = new HallLanceMemoryCalculator();

Then you can call the instance's getCurrentValue():

然后你可以调用实例的getCurrentValue()

double value = me.getCurrentValue();

回答by hola

Create an instance of your HallLanceMemoryCalculatorthen call getCurrentValue()or make getCurrentValue()static.

创建一个HallLanceMemoryCalculatorthen 调用的实例getCurrentValue()或使其成为getCurrentValue()静态。

回答by Andrew

Static means there is one for an entire class, whereas if it is non-static there is one for each instance of a class (object). In order to reference a non-static method from a static context, you need to first create an object for that method to be a part of. So, in your main method (the static context), you need to create a new HallLanceMemoryCalculator object. Once you have the object, you can use the object's methods.

静态意味着整个类都有一个,而如果它是非静态的,则类(对象)的每个实例都有一个。为了从静态上下文中引用非静态方法,您需要首先为该方法创建一个对象作为其一部分。因此,在您的主方法(静态上下文)中,您需要创建一个新的 HallLanceMemoryCalculator 对象。拥有对象后,就可以使用该对象的方法。

The reason your professor does not want it to be static, is so that you have the ability to have multiple HallLanceMemoryCalculator instances, that each keep track of their own value.

您的教授不希望它是静态的原因是,您可以拥有多个 HallLanceMemoryCalculator 实例,每个实例都跟踪自己的值。

Note, I'm not including any code, because I'm sure your professor would want you to figure out that part on your own.

请注意,我不包括任何代码,因为我确定您的教授希望您自己弄清楚那部分。

回答by Simon Zambrovski

The Method getCurrentValue() is defined as an ordniary (non-static) method of the class. In order to execute it, you need an instance of HallLanceMemoryCalculator.

方法 getCurrentValue() 被定义为类的普通(非静态)方法。为了执行它,您需要一个 HallLanceMemoryCalculator 实例。

So try to instantiate HallLanceMemoryCalculator first:

所以先尝试实例化 HallLanceMemoryCalculator:

    HallLanceMemoryCalculator calc = new HallLanceMemoryCalculator();
    double value = calc.getValue();

In order to make some sense, the example should have a constructor for storing the initial value. E.g.

为了有意义,该示例应该有一个用于存储初始值的构造函数。例如

    public HallLanceMemoryCalculator(double initial) {
        this.currentValue = initial;
    }

In doing so, you can use the following main code:

为此,您可以使用以下主要代码:

    HallLanceMemoryCalculator calc = new HallLanceMemoryCalculator(10);
    int choice = displayMenu();

    // some code to get the second operand (you don't need the string as param)
    double operand = getOperand("");

    // some switch statement to handle the choice
    ...

    double result = calc.getCurrentValue();

回答by Sofiane

you should use an instance of the class and not the class if you want to call your getter function without setting the attribute static.

如果你想在不设置属性的情况下调用你的 getter 函数,你应该使用类的实例而不是类static