我的错误代码如下:
public class Calculator {
private int integer1;
private int integer2;
private char operator;
public Calculator() {
}
public Calculator(int integer1,char operator,int integer2) {
this.integer1 = integer1;
this.operator = operator;
this.integer2 = integer2;
}
public static void main(String[] args) {
Calculator c1 = new Calculator(integer1,operator,integer2);
try {
System.out.println("计算结果:" + c1.calculate());
}catch (ArithmeticException ae) {
System.err.println("产生异常:" + ae);
}catch (UnsupportedOperationException ue) {
System.err.println("没有这种运算!:" + ue);
}finally {
System.out.println("主方法执行结束");
}
}
Calculator c1 = new Calculator(integer1,operator,integer2);这句报错:Cannot make a static reference to the non-static,原因是括号内的变量是静态变量,无法直接调用,于是我把Calculator c1 = new Calculator(integer1,operator,integer2);中的变量去除,变成Calculator c1 = new Calculator();
就可以运行程序。
这样的错误,下次再遇到,大家可以直接把变量改成静态的,或者实例化对象,然后使用对象名.变量名来调用。

本文介绍了在Java中遇到静态方法调用错误的案例,通过实例化对象并使用对象名调用非静态变量的方法,解决了Calculator类中静态引用非静态变量的问题。建议遇到类似问题时,检查变量类型并适当地创建对象进行操作。
1272

被折叠的 条评论
为什么被折叠?



