Android, couldn't make static getter setter property work?

不打扰是莪最后的温柔 提交于 2019-12-24 09:17:14

问题


I was trying this ...

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
    }

}

in any other class...

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

It just doesn't work - what could the problem be? Tried everything.


回答1:


Why do you define the setter/getter if you are going to end up doing this?

Info.currentIndex = 666;

if so, then change currentIndex visibility to public...

or even better, be congruent with the code and do

Info.setCurrentIndex(666);


来源:https://stackoverflow.com/questions/40852452/android-couldnt-make-static-getter-setter-property-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!