Android,无法使静态getter setter属性工作?

时间:2016-11-28 19:58:24

标签: java android properties getter-setter

我正在尝试这个......

public class Info {
    private static Info ourInstance = new Info();
    public static Info getInstance() { return ourInstance; }

    private static int currentIndex;

    public static void setCurrentIndex(int i) {
        Log.d("DEV", "setter!");
        currentIndex = i;
        // do other work here
    }

    public static int getCurrentIndex() {
        Log.d("DEV", "getter!");
        return currentIndex;
    }

    private Info() {
        Log.d("DEV", "class initialized no problem...");
        currentIndex = 42; // just doesn't work, only sets the field
    }

}

在任何其他班级......

Info.currentIndex = 666; // just doesn't work

它只是不起作用 - 问题是什么?尝试了一切。

1 个答案:

答案 0 :(得分:2)

如果你最终会这样做,为什么要定义setter / getter?

Info.currentIndex = 666;

如果是,请将 currentIndex 的可见性更改为公开...

甚至更好,与代码一致并执行

Info.setCurrentIndex(666);