-1

I want to store an object state between activities (already considered Parcelables, JSON, yadda yadda) but since I have a couple of Singletons, might as well refer to them in a class that extend Application (modularity + easy to maintain).

So to my question, let's say I have a simple singleton:

class SimpleSingleton
{
    private static final SimpleSingleton instance;   //The question will refer this line later.
    public static SimpleSingleton getInstance()
    {
        return instance;
    }

    private SimpleSingleton(){}
}

1: At first I create an initInstance() method within the above class, e.g:

class SimpleSingleton
{
    //... the code above

    public static void initInstance()
    {
        if(instance == null) instance = new SimpleSingleton();
    }
}

2: Hence the below works, (in which afterwards, I can refer to the singleton from any activity via CustomSingleton.getInstance()):

class MyApp extends Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();
        initSingletons();
    }

    protected void initSingletons()
    {
        SimpleSingleton.initInstance();
    }
}

BUT. What if I declare

private static final SimpleSingleton instance = new SimpleSingleton();

instead of

private static final SimpleSingleton instance;

in the SimpleSingleton class?

I assume the object is initialized during compile time, so doesn't that makes the whole #1 and #2 unnecessary? Or do I get the order wrong (especially WHEN the class is actually initialized)? I came from C# and currently developing for Android so this kinda gave me a quick gotcha when I want to refer to my Singletons. Also, I ask this since according to this blog:

The explanation of the weird behavior I saw that makes more sense to me is that the static variables instances are bound to the class loader of the class that first initialized them.

1
  • I think there is a lot of confusion in the article, static variable instance are indeed bound to the class loader of the class that first initialized them, that is, or should be, the singleton class itself. Actually I am more prone to think that the author had some kind of bugs in his code that made unclear why singleton instances would change over time, and diagnosed that instantiating the variables in another place "works" as a workaround. I always used singletons in android without needing to initializing them in other places than the class itself, and I never had any such problem. Commented Sep 13, 2014 at 7:42

1 Answer 1

1

The only difference i can think of is when you do

private static final CustomObject instance = new CustomObject();

when you application is launched it will create and allocate space for it. Note it might never be used but it would still be using memory.

when you create it on an onCreate method it will only create an instance when it is called.

Using static also has one more disadvantage that is it will use your perm gen space and if by chance it fails to give it space or fails to create it your program will crash on startup. Leaving you confused.

I strongly suggest using the onCreate method approach.

Sign up to request clarification or add additional context in comments.

1 Comment

Whoops. My bad. Forgot to replace the CustomObject with SimpleSingleton. Edited the code. I accepted your answer as I would go with the OnCreate approach (for now), especially since you brought up another problem I haven't thought about for initializing a number of singletons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.