我已经研究这个问题有一段时间了,我确信修复就在我面前,但我只是对它视而不见。有人愿意指出为什么我的 int i 给我一个错误吗?
CacheDownloader 类:
public static void main(String[] args) {
String strFilePath = "./version.txt";
try
{
FileInputStream fin = new FileInputStream(strFilePath);
DataInputStream din = new DataInputStream(fin);
int i = din.readInt();
System.out.println("int : " + i);
din.close();
}
catch(FileNotFoundException fe)
{
System.out.println("FileNotFoundException : " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
}
}
private final int VERSION = i;
错误:
CacheDownloader.java:54: error: cannot find symbol
private final int VERSION = i;
^
symbol: variable i
location: class CacheDownloader
最佳答案
您必须在 try-catch
block 之前声明 int i
。另外,您必须在 main
方法中声明常量:
public static void main(String[] args) {
String strFilePath = "./version.txt";
int i;
try
{
FileInputStream fin = new FileInputStream(strFilePath);
DataInputStream din = new DataInputStream(fin);
i = din.readInt();
System.out.println("int : " + i);
din.close();
}
catch(FileNotFoundException fe)
{
System.out.println("FileNotFoundException : " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
}
final int VERSION = i;
}
关于java - 错误: cannot find symbol - Java compile error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30880396/