Java (单独类中的变量)无法解析或不是字段
我遇到了这个我以前从未见过的问题。我在Java (单独类中的变量)无法解析或不是字段,java,Java,我遇到了这个我以前从未见过的问题。我在Variables类中创建了一个整数变量。在Run类中,我想检查它的值,如果它大于0,则打印“Hello”。但是,编译器给出错误:“var1无法解析或不是字段”。请注意,我不想在运行类上调用扩展变量。我想在不扩展的情况下这样做 我有一个简单的代码: public class Run { if (Variables.var1 > 0) { //var1 is underlined, giving the error "var1 cannot
Variables
类中创建了一个整数变量。在Run
类中,我想检查它的值,如果它大于0,则打印“Hello”
。但是,编译器给出错误:“var1无法解析或不是字段”
。请注意,我不想在运行
类上调用扩展变量
。我想在不扩展的情况下这样做
我有一个简单的代码:
public class Run {
if (Variables.var1 > 0) { //var1 is underlined, giving the error "var1 cannot be resolved or is not a field"
System.out.println ("Hello.");
}
}
其他类别:
public class Variables {
public static int var1 = 1;
}
我不知道这是否是故意的,但这应该是:
public class Run {
public void aMethod2Call(){
if (Variables.var1 > 0) {
System.out.println ("Hello.");
}
}
}
您没有使用正确的java语法。请尝试以下操作:
public class Run {
// a If instruction must be inside in a method
public void someMethod(){
if (new Variables().var1 > 0) {
//var1 is underlined, giving the error "var1 cannot be resolved or is not a //field"
System.out.println ("Hello.");
}
}
}
变量是否在同一个包中?如果没有,您是否已导入?如果是这样,请尝试重建您的project@BohemianVariables
是一个不同的类,但它在同一个包中。已编辑。语法错误,不能调用,如果在方法之外,Java类必须是一组字段、构造函数和方法声明
public class Run {
// a If instruction must be inside in a method
public void someMethod(){
if (new Variables().var1 > 0) {
//var1 is underlined, giving the error "var1 cannot be resolved or is not a //field"
System.out.println ("Hello.");
}
}
}