所以我在尝试比较 Main 类中声明的两个字符串时遇到了一些问题。我搞砸了它,我真的无法让它工作!问题出在我比较变量的 if() 语句中......
public class Main {
public String oldContent = "";
public String newContent = "";
public static void main(String[] args) throws InterruptedException {
Main downloadPage = new Main();
downloadPage.downloadPage();
oldContent = newContent;
for (;;) {
downloadPage.downloadPage();
if (!oldContent.equals(newContent)) { // Problem
System.out.println("updated!");
break;
}
Thread.currentThread().sleep(3000);
}
}
private void downloadPage() {
// Code to download a page and put the content in newContent.
}
}
最佳答案
变量是实例成员,而 for 发生在静态方法中。 尝试将实际函数移动到实例方法(不是静态的),或者相反地使数据成员也成为静态的。
关于Java:无法访问变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5687887/