1

This is the example:

http://www.tutorialspoint.com/java/java_using_singleton.htm

When looking at the 1st example, when user writes : Singleton.getInstance() , then it calls out :

new Singleton()

I don't get it, how it is singleton, when everytime it creates a new singleton object?

I understand the 2nd example. If singleton is null, then create new object, but in first example, it always creates new object??

Whats up with that?

Jaanus
  • 16,161
  • 49
  • 147
  • 202
  • The second pattern is not thread safe, btw. You could end up with multiple instances, and if the instances have state that isn't itself thread safe, you could see an instance as partially constructed. – yshavit Sep 20 '12 at 20:40
  • yes, the second example should probably have the synchronized keyword on the getInstance() method. There's slightly more efficient ways to do it which don't have as much overhead, but that's the simplest way. – Matt Sep 21 '12 at 03:54

5 Answers5

10

No, in the first example the only call to new Singleton() is here (within Singleton):

private static Singleton singleton = new Singleton( );

That's a static variable initializer. It gets executed once, and only when needed. (If you never touch the Singleton class, the initializer won't get executed.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • It goes for every static variable? static variable is only executed once? for example I have `private static long time = System.currentTimeMillis()` , then it will always be the same? – Jaanus Sep 20 '12 at 20:53
  • @Jaanus: Within one classloader, yes. – Jon Skeet Sep 20 '12 at 20:56
  • @Jaanus: I don't understand your question. A class is loaded by a classloader, and will only be initialized once for that classloader. If a different classloader loads the same class, it will be initialized again. – Jon Skeet Sep 21 '12 at 05:56
  • Just having troubles understanding what is a classloader exactly? – Jaanus Sep 21 '12 at 05:58
  • 1
    @Jaanus: Have you tried searching? I just tried a quick search, and found a bunch of useful hits. To be honest, if you're a beginner you probably don't need to worry about it to start with. It's mostly important to understand in web applications, where different apps in the same container are loaded by different classloaders. – Jon Skeet Sep 21 '12 at 06:00
1

In the second example, as someone mentioned, it's not thread safe. Here's two options for making the getInstance() method thread safe:

 public static synchronized Singleton getInstance() {
     if (singleton == null) {
         singleton = new Singleton();
     }
     return singleton;
 }

 public static Singleton getInstance() {
     if (singleton == null) {
         synchronized(Singleton.class) {
             if (singleton == null) {
                 singleton = new Singleton();
             }
         }
     }
     return singleton;
 }

The second version of the method allows you to get an already constructed singleton instance without any synchronization overhead. However, when it is null, you then enter a synchronized block. You check the null again just in case there was a thread context switch and a second thread actually gets into the synchronized block before you do. That ensures only a single instances is ever constructed.

Either approach should be entirely thread safe.

Matt
  • 11,523
  • 2
  • 23
  • 33
  • Must I check if `(singleton == null)`, the first time, or I can enter synchronized block instantly? – Jaanus Sep 21 '12 at 05:54
  • 1
    The first check is what allows you to avoid the overhead of synchronization when the object already exists (which will be every call but the first). If you're going to remove that, you might as well just use the synchronized keyword on the method instead of the synchronized block. – Matt Sep 21 '12 at 10:36
0

The new Singleton() is only getting called when the class is loaded.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
0

The member singleton is declared as static, so new Singleton() is only ever called once.

dckrooney
  • 3,041
  • 3
  • 22
  • 28
0

The line:

private static Singleton singleton = new Singleton( );

only executes once when the class is loaded -- it's a class field initializer.

Look at this question: Best Practice: Initialize class fields in constructor or at declaration? -- it's the same idea with the new Random() in that question.

Edit: I popped in to Stack Overflow for a moment to see if I could answer an unanswered question, and before I could submit my response, it looks like three others already have answered it. I like Stack Overflow!

Community
  • 1
  • 1
Jim Flood
  • 8,144
  • 3
  • 36
  • 48