If using INSTANCE holder class to implement Singleton Pattern, the compiler will generate another class file named classname$1.class. You can find the question by this link: here . So my question is what is the solution for Singleton Pattern? double check?
4 Answers
You can use simple enumeration.
public enum Singleton {
INSTANCE;
}
You can access the INSTANCE
simply as Singleton.INSTANCE
rather than calling a getInstance()
method. And by default , creation of Enum instance is thread safe.

- 48,828
- 16
- 130
- 164
-
Yes, it is a solution, but it is a little bit wired. – handrenliang Jun 09 '13 at 06:59
-
@handrenliang this is better than having a `private static final Type instance = new Type();` and a `public static Type getInstance() { return instance; }` (or another ways to implement Singleton). – Luiggi Mendoza Jun 09 '13 at 07:00
-
But Enum cannot extend Enum or class in Java – handrenliang Jun 09 '13 at 07:08
-
@Luiggi -- it's not better at all, it's misleading and wrong. That's not what enums are for and you really shouldn't be doing that. – Software Engineer Jun 09 '13 at 07:12
-
@handrenliang May be [this](http://stackoverflow.com/questions/11452969/dealing-with-singletons-which-have-to-subclass) helps . – AllTooSir Jun 09 '13 at 07:12
-
2@EngineerDollery the problem is about the design of the singleton. To extend from a Singleton just give you the idea of bad design. Notice than this is a reason why `enum`s exists: to **prevent** this kind of bad design. In this case, it would be better for OP to explain his/her real problem instead of asking *how can I make the Singleton pattern work?* – Luiggi Mendoza Jun 09 '13 at 07:16
-
Enums do not exist for that reason at all. Go read the jls, then get back to me. – Software Engineer Jun 09 '13 at 07:26
-
1@EngineerDollery JLS don't tell you this, you learn it from application design experience. – Luiggi Mendoza Jun 09 '13 at 07:30
-
The jls tells me what an enum is. It doesn't even hint that you can or should use this as a singleton. I know this because I read it regularly. It would be hard to imagine anyone, other than the designer of enums even thinking about such a thing. – Software Engineer Jun 09 '13 at 07:48
-
see also http://stackoverflow.com/questions/5759596/the-best-singleton-pattern-since-java-5 – keuleJ Jun 09 '13 at 08:45
You seem to be asking how this singleton code is implementing thread-safe initialization:
public final class Test {
static final class TestHolder {
private static final Test INSTANCE = new Test();
}
private Test() {}
public static Test getInstance() {
return TestHolder.INSTANCE;
}
}
The solution's thread-safeness is guaranteed because:
- static initialization is performed in a thread-safe fashion, and
- once initialized, a
final
variable can be used without further synchronization.
It is not doing double-checked locking.
Note that this particular pattern is thread-safe AND lazy initialized. If you want thread-safe singleton with non-lazy initialization then:
public final class Test {
private static final Test INSTANCE = new Test();
private Test() {}
public static Test getInstance() {
return INSTANCE;
}
}
The "enum
as singleton" approach is also acceptable ... despite what Engineer Dollery says.
Lazy initialization using double-checked locking can be implemented correctly from Java 5 onwards (if you do it right using a volatile
), but in Java 1.4.x and earlier the idiom is broken.

- 698,415
- 94
- 811
- 1,216
-
If class Test has some static method, INSTANCE will be initialized too. – handrenliang Jun 09 '13 at 07:48
-
-
if you have a static method in Test, then call the method, INSTANCE is initialized because the class is loaded. – handrenliang Jun 09 '13 at 07:54
-
That is correct for the 2nd (non-lazy) example. If you don't want that to happen, don't put (other) static methods in your Singleton classes ... or don't call them :-). But in the 1st (lazy) example, a call to some other static method in the outer class doesn't trigger static initialization of the inner class. The inner class initialization is only triggered when the `getInstance()` method attempts to access the inner classes static field. – Stephen C Jun 09 '13 at 10:44
I would go with enum after jdk 1.5. Check htis link for details : http://javarevisited.blogspot.in/2012/07/why-enum-singleton-are-better-in-java.html
- Double check locking is not advised , it doesn't give performance.
- The solution in the link you mentioned is an efficient solution using static inner class, as it doesn't use synchronize and uses lazy loading but enum is easiest to implement along with performance.
Below article explains why double check locking can be a bad choice[as Thomas pointed out, that Double check locking issue mentioned in beow article was fixed with Java 1.5] : http://www.ibm.com/developerworks/java/library/j-dcl/index.html

- 7,810
- 6
- 48
- 78
-
+1 for explaining benefits of using `enum` against Singleton pattern implementation. – Luiggi Mendoza Jun 09 '13 at 07:03
-
-
1What are you tryig to do? You want to extend a class and make it singleton? Looks like a bad design. – Lokesh Jun 09 '13 at 07:15
-
This is a dreadful idea. You're completely wrong in every possible way. You can't base such a dumb idea on one blog post! Do you even know what a pattern is? Have you ever heard of an idiom? – Software Engineer Jun 09 '13 at 07:24
-
@handrenliang it would be better if you explain your real problem instead of asking how to make a Singleton patter implementation. – Luiggi Mendoza Jun 09 '13 at 07:24
-
@EngineerDollery you can't extend an `enum` but an `enum` vale can `override` methods declared in the `enum` definition. – Luiggi Mendoza Jun 09 '13 at 07:27
-
2@EngineerDollery: Effective Java by Joshua Bloch talks about singleton using enum. Thats a standard reference. – Lokesh Jun 09 '13 at 07:30
-
Josh Bloch's book is a standard reference, you're right, but Josh has been wrong before many times and isn't infallable, and he's not considered an expert on idomatic use of java (in fact, if you follow his advice, we'd have getters and setters everywhere and DDD wouldn't exist). An enum is an enumeration, not a singleton. When a developer sees an enum they think it's an enum, not a singleton -- it's not an idiom, it's just plain dumb. – Software Engineer Jun 09 '13 at 07:35
-
@LuiggiMendoza I know there are many ways to implement Singleton Pattern, I just want to know which one is the best in most situation. – handrenliang Jun 09 '13 at 07:38
-
1@handrenliang the best would be *don't use Singleton, use an `enum` or a third party agent that handles the single registration for you like Spring or EJB `@Singleton`*. – Luiggi Mendoza Jun 09 '13 at 07:41
-
@EngineerDollery I agree with you. When using Enum, you might rely on comments to explain that is an Singleton Pattern, not a real Enum. – handrenliang Jun 09 '13 at 07:41
-
Using an enum for a singleton is one of the most stupid ideas I've heard in my 35 years in this industry. If a programmer working for me did this we'd be having a very serious talk about their qualifications for the job -- even if it were josh bloch -- and I have lots of programmers working for me. I've never seen anybody try something so misleading when creating a singleton (in fact, on my teams, singleton is an antipattern, because of the trouble it causes) – Software Engineer Jun 09 '13 at 07:47
-
@EngineerDollery the fact that this singleton class can't even be inherited makes it a good example to be replaced by an `enum`. If not, then show why this can't be done (note: for current OP's code example). – Luiggi Mendoza Jun 09 '13 at 07:49
-
1@EngineerDollery do you have any arguments besides "it is stupid"? I think you could see a singleton as a special case of enum, that has just one value. loki explained the advantages in his answer. – keuleJ Jun 09 '13 at 08:59
-
1The [developerWorks article](http://www.ibm.com/developerworks/java/library/j-dcl/index.html) you cite on why double check locking is broken is seriously outdated (it's from 2002). Double check locking was fixed with Java 1.5 in 2004. Since then, if done right, it's ok. – barfuin Jun 09 '13 at 09:23
Depending on your deployment architecture it can become very difficult to implement a singleton in Java. When you're talking about singletons you also have to talk about their scope/context. Because few people understand this singletons have often been called evil and are considered anti-patterns.
If you're deploying to a single jvm, outside an application server, then the idiomatic approach is to implement a private final static instance of the class within itself and expose this through a suitably named method (getInstance() is common), and make the constructor of the class private. This is a singleton constrained to a single classloader and a single jvm.
However, this situation is incredibly uncommon.
In all other situations, you have to consider the classloader problem and the problem of having your code deployed to multiple jvms/servers. In a typical Java EE application container each war is usually loaded using its own classloader. If each war relies on a jar containing a singleton, they will each get their own copy of that singleton. This could be ok, if the singleton is for, say, a DB connection and you're allowed lots of them. But, if the singleton is supposed to protect access to a limited resource, then this isn't going to work well because you'll have one class capable of accessing that resource per webapp. The scope of your singleton here is container-scope.
It gets worse in full enterprise deployments, with clusters and live failover. In this environment you have many copies of your software running simultaneously. It's hard to imagine a java only singleton working at this scope -- the enterprise scope. At this level you need to figure out what the single source of truth is for singleton creation, or execution (if it's ok to have multiple instances but only one of them can be processing requests at a time), and all the singletons you create of the same type must defer to the single source of truth -- an enterprise semaphore.

- 37,782
- 12
- 108
- 140

- 15,457
- 7
- 74
- 102
-
After all this explanation, looks like implementing Singleton is a bad idea... – Luiggi Mendoza Jun 09 '13 at 07:19
-
Well, yes. Spring uses something called Singleton by default on all of the beans you create, but they're quite clear to define the scope to a single application context. – Software Engineer Jun 09 '13 at 07:21
-
Spring doesn't explicitly uses this kind of singleton, it means that it will keep a single instance of the object references of the managed beans, so if you undeploy the application, these instances will be removed from the JVM as well. – Luiggi Mendoza Jun 09 '13 at 07:26
-
That's not what that means at all. You are free to keep references to spring beans outside of the application context and the instances can't be removed from spring no matter how hard it tries. Also, they're not removed from the jvm either -- classloaders rarely remove a class once it's loaded. All spring does is try to remove those references to the bean that it knows about, nothing else. – Software Engineer Jun 09 '13 at 07:31
-
Again, Spring Singleton is about **having a single instance of the managed bean**, it is not a `static final` instance as you may think. By the way, I never said anything about class unloading in my last comment. – Luiggi Mendoza Jun 09 '13 at 07:35
-
And I didn't says that spring uses static finals. I don't think I said that, let me review my comments... No, never even implied it. – Software Engineer Jun 09 '13 at 07:40
-
Then how do you explain Spring having a single instance of the class that would be *removed* only when unloading the class based on that comment? – Luiggi Mendoza Jun 09 '13 at 07:42
-
If you have a clear question please write a question -- this is a discussion and this isn't the place for that. – Software Engineer Jun 09 '13 at 07:53