Java:无法访问变量?

标签 java variables

所以我在尝试比较 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/

相关文章:

java.io.FileNotFoundException : read. txt (系统找不到指定的文件)`

java - Apple AWT 内部异常 : NSWindow drag regions should only be invalidated on the Main Thread

javascript - 在 InnerHTML 中调用的函数中的变量错误

c - 跨不同共享库使用的全局变量

Java Spring 查询

java - 根据另一列值从总和中减去或添加值

java - 找出可以用 1 和 0 写成的数的倍数

PHP:查询超过最大时间(如何处理)

database - 范围变量隐藏封闭 block LINQ 中的变量

javascript - 用于登录的本地存储