Search code examples
javaandroidpropertiesgetter-setter

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


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.


Solution

  • 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);