0

For example, I have a class

public class EagerInitializedSingleton {
        private static final EagerInitializedSingleton instance = new EagerInitializedSingleton();

        public static EagerInitializedSingleton getInstance(){
            return instance;
        }
}

And my application have 2 activity A.java and B.java (from A I can go to B).
In B activity I have

import EagerInitializedSingleton.java;

public class B{
     onCreate(...){
         EagerInitializedSingleton.getInstance()...
     }
}

My question is when instantiated be instantiated`

  • When launch application (before A Activity start)
  • When import EagerInitializedSingleton.java
  • Or when EagerInitializedSingleton.getInstance()

If possible, can I check when be instantiated by write Log or something? Any help would be great appreciated.

UPDATE I'm follow here to create EagerInitializedSingleton http://www.journaldev.com/1377/java-singleton-design-pattern-best-practices-examples
And they have say

If your singleton class is not using a lot of resources, this is the approach to use. But in most of the scenarios, Singleton classes are created for resources such as File System, Database connections etc and we should avoid the instantiation until unless client calls the getInstance method

Like some answer say that instance be instantiated when I call EagerInitializedSingleton.getInstance()..., so who is correct?

Linh
  • 57,942
  • 23
  • 262
  • 279

3 Answers3

3

static variables are initialized when the classloader loads the class for the first time, either through static reference or instance creation. It will be shared across all instances of the class. And remember, it will be initialized before any instance creation of the class.

So, in your question:

When launch application (before Activity start)

No

When import EagerInitializedSingleton.java

No

When EagerInitializedSingleton.getInstance()

Yes

Or whenever you make a static reference to the EagerInitializedSingleton class.

Edit - Just to clear things out as per comments:

Call to getInstance() will not result in instance creation. But the static reference to the class does as the class loads for the first time.

Rohit Arya
  • 6,751
  • 1
  • 26
  • 40
  • when he calls `EagerInitializedSingleton.getInstance()` only `instance` varaiable is returned, nothing else – pskink Sep 14 '16 at 08:29
  • `Static fields are initialized when the class is loaded by the class loader. Default values are assigned at this time. This is done in the order than they appear in the source code.` – pskink Sep 14 '16 at 08:36
  • @pskink, yes instance will be returned but `new EagerInitializedSingleton()` will be called first. – Rohit Arya Sep 14 '16 at 08:41
  • but it will not be called as a result of `getInstance` method call – pskink Sep 14 '16 at 08:43
  • @pskink, yes I have suggested the same thing in my answer. :) I am not saying that singleton implementation is correct. I am just trying answer when the instance will be created. – Rohit Arya Sep 14 '16 at 08:43
  • so it will be created when the class is loaded by the class loader, not `When EagerInitializedSingleton.getInstance()` gets called – pskink Sep 14 '16 at 08:44
  • @pskink, I agree to what you are pointing. That `getInstance()` method **will not** create instance. I am saying that `static` reference to the class **before** actually invoking the method will result in instance creation. – Rohit Arya Sep 14 '16 at 08:47
  • @RohitArya please help me one more, can you explain why the OP of journal dev say `we should avoid the instantiation until unless client calls the getInstance method` – Linh Sep 14 '16 at 09:15
  • Yes, they have pointed it right. In that case, all resources should be instantiated (by calling new SomeClass()) inside (say) a private constructor of the singleton class. and thus when we invoke getInstance method, it will call the private constructor to initialize those. But if you create some static variables like the one in the question, they will get initialized even with static reference of the class. So basically, it's all dependent on the implementation of singletons. – Rohit Arya Sep 14 '16 at 13:02
1

When you call EagerInitializedSingleton.getInstance()

Cryperian
  • 116
  • 1
  • 8
1

For starters, this is not a proper singleton implementation. Your constructor, or lack of one, will allow the user to use the default empty constructor and create more objects of that class. Check out how to implement it here or anywhere you find online.

The question you asked has nothing to do with android, it's a plain Java question, having to do with static variables initialization. You can find the answer to that question here.

Community
  • 1
  • 1
Vucko
  • 7,371
  • 2
  • 27
  • 45
  • thank you. I visit this link but still confusing, please check my update. Does the owner of the link correct or @Rohit Arya answer is correct – Linh Sep 14 '16 at 08:41
  • @pskink like the java link they say `static variables are initialized only once , at the start of the execution`. does execution here mean when application start – Linh Sep 14 '16 at 08:43
  • Owner of the link explains **generally** and Rohit answers your question. And he has answered your particular question. I am just telling you that you need to fix your implementation of singleton, and then you will not even have this question (because it's not a proper way to initialize the instance there anyway). – Vucko Sep 14 '16 at 08:46
  • yes, @Vucko is right. This is not a proper way of creating singletons. But I answered when the instance will actually be created. :) – Rohit Arya Sep 14 '16 at 08:49
  • I don't ask about how to create the singleton, I just ask when instance variable is initialized :( – Linh Sep 14 '16 at 08:50
  • @PhanVanLinh, yes then you can refer my answer. :) – Rohit Arya Sep 14 '16 at 08:50
  • if you don't mind, please check my update in my question. in there it have this sentence `we should avoid the instantiation until unless client calls the getInstance method`. So with @RohitArya answer, this sentence is completely wrong right? – Linh Sep 14 '16 at 08:51