使用静态方法时出现“无法找到符号”错误

时间:2014-09-28 18:51:41

标签: java static-methods

我的代码非常简单,我删除了奇数代码。

所以这是我的班级,他的一个方法是静态的,我想稍后在Main class中使用它:

public  class  TradeInformationReader {

 private static String tradeType = "FX_SPOT";
 public static double tradePrice = -1;
 private double price;

 public  static int setTradeInformation(String path_to_file) {
 return 1;
  }
 }

这是我试图调用最后一种方法的方法:

public class Main {

public static int main(String[] args) {

    String path_to_file = "D:\\1.txt";
    if (0 > TradeInformationReader.setTradeInformation(path_to_file)) {
        return -1;
    }

    return 1;
 }
}

我阅读了许多类似问题的帖子,但找不到解决方案。一切看起来都很好。 IDE没有显示任何错误,我只是想调用静态方法setTradeInformation,为什么它不能识别它(找不到符号方法setTradeInformation)?有任何想法吗?我将非常感谢你的帮助。

2 个答案:

答案 0 :(得分:7)

你的main不是有效的main,所以我猜你的IDE找不到启动类。这应该是

public static void main(String[] args)

答案 1 :(得分:2)

首先,您必须将TradeInformationReader类放在一个名为:TradeInformationReader.java的单独文件中

如下:`

public  class  TradeInformationReader {

 private static String tradeType = "FX_SPOT";
 public static double tradePrice = -1;
 private double price;

 public  static int setTradeInformation(String path_to_file) {

    //integer to identify whether the file is found or not 1 if found and 0 if not
    int isFileFound = 1;

    // the code required to get the file and modify the state of the of isFileFound variable

    return isFileFound;
  }
 }

`

然后主类应该具有void返回类型,并且应该在与Main Class同名的文件中,如下所示:

 public class firstApp {

 public static void main(String[] args) {

String path_to_file = "D:\\1.txt";
if (0 > TradeInformationReader.setTradeInformation(path_to_file)) {
    System.out.println("File not found");
}

} } `