An immutable object is initialized by its constuctor only, while a singleton is instantiated by a static method. How to make an immutable singleton in Java?
4 Answers
while a singleton is instantiated by a static method
While this is the usual way of doing it, this is by no means the only way.
In Java 1.5 a new version of Singleton is the enum singleton pattern:
public enum Elvis{
INSTANCE // this is a singleton, no static methods involved
}
And since enums can have constructors, methods and fields, you can give them all the immutable state you want.
Reference:
- Java tutorial: Enum Types
- Effective Java, Item 3
- Singleton (the enum way) (WikiPedia)
Also, the term Singleton leaves some room for interpretation. Singleton means that there is exactly one object per defined scope, but the scope can be a number of things:
Java VMClassloader (thanks @Paŭlo Ebermann for reminding me): in this case use enums or the initialize-through-static-inner-class pattern. This is of course what is usually meant by a singleton.
Be Careful: enums and all other singletons are broken if loaded through multiple Classloaders.- Enterprise Application (in this case you need a container-managed singleton, e.g. a Spring singleton bean). This can be several objects per VM or one object per several VMs (or one Object per VM, of course)
- Thread (use a
ThreadLocal
) - Request / Session (again, you'll need a container to manage this, Spring, Seam and several others can do that for you)
- did I forget anything?
All of the above can be made immutable, each in their own way (although it's usually not easy for container-managed components)

- 292,901
- 67
- 465
- 588
-
This example is taken from Effective Java, I presume ;-) – darioo Feb 03 '11 at 14:59
-
@darioo: yes, but it's missing the `leaveTheBuilding()` method :-) – Sean Patrick Floyd Feb 03 '11 at 15:00
-
by the way, has singleton broken some OOP principle? – chance Feb 03 '11 at 15:03
-
@wang Some say yes, because it introduces global state. Read the Wikipedia article: http://en.wikipedia.org/wiki/Singleton_pattern – Sean Patrick Floyd Feb 03 '11 at 15:08
-
Actually in the first case the scope is not **Java VM**, but classloader. If you load the same class again with another classloader, it can have it's own singleton. – Paŭlo Ebermann Feb 03 '11 at 15:50
The solution pointed out by Sean is a good way of initializing singletons if their creation is not expensive. If you want to "lazy loading" capability, look into the initialization on demand holder idiom.
// from wikipedia entry
public class Singleton {
// Private constructor prevents instantiation from other classes
private Singleton() {
}
/**
* SingletonHolder is loaded on the first execution of Singleton.getInstance()
* or the first access to SingletonHolder.INSTANCE, not before.
*/
private static class SingletonHolder {
public static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}

- 22,857
- 4
- 59
- 71
-
yup, that's the one I referred to as 'initialize-through-static-inner-class pattern' :-) +1 – Sean Patrick Floyd Feb 03 '11 at 15:10
-
@Sean: Oh, I didn't realize that you were referring to the same thing. I guess your answer was indeed complete. +1 :) – Sanjay T. Sharma Feb 03 '11 at 15:33
public enum MySingleton {
instance;
//methods
}
//usage
MySingleton.instance.someMethod();

- 12,753
- 4
- 37
- 58
You're being unnecessary complicated. To be immutable an object must be unmodifiable once it is created. That's normally interpreted to mean "modifiable only in the constructor", but if you were to create it another way that would still make it immutable. As long as your object cannot be modified after it is initialized then it is immutable. You can consider setting up the Singleton instance to be part of the initialization.
Most of the benefits of immutability are irrelevant in Singletons.

- 26,349
- 9
- 53
- 79