3

As per Josh Bloch's Effective Java Enum is the best way to implement Singleton pattern.

Java enum takes care of Serialization and Thread Safety on its own. But I do not understand how.

Can someone please explain it?

I had looked at this and this.

The second link gives good amount of details on enum being capable of serialization but I am not getting sufficient explanation on thread-safety.

Also are the methods declared in enum are thread-safe or any special care has to be taken?

Community
  • 1
  • 1
Sam
  • 2,352
  • 4
  • 32
  • 45

2 Answers2

4

static initializer blocks are single threaded using a Lock internal to the JVM. i.e. it doesn't appear in a jstack trace like other locks. No other thread can access anything the class until it has been initialized. This is true of all classes, not just enum.

enum are initialized in a static initializer block generated by the compiler.

Also are the methods declared in enum are thread-safe or any special care has to be taken?

Again, enum are just like regular classes. Only the methods you mark as synchronized are synchronized. Note: just adding synchronized doesn't make it thread safe. ;)

While enum are effectively static final instances, you can modify their fields ie. the fields on an enum are not final by default. Also you can modify enums by messing with the internal structure of the class using reflection and Unsafe.allocateInstance. Generally this is not a good idea.

From JLS 8.9.2

for each declared enum constant with the name n, the enum type has an implicitly declared public static final field named n of type E. These fields are considered to be declared in the same order as the corresponding enum constants, before any static fields explicitly declared in the enum type.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    Can you please point me to a resource which says this "enum are initialised in a static initialiser block generated by the compiler." officially? I mean tried finding but could get one. – Sam Feb 13 '14 at 08:14
  • As a follow up question, can all this be done in a non-enum class and make it as robust singleton as enums? – Miserable Variable Feb 13 '14 at 08:14
  • @MiserableVariable I think yes. But you have to a lot of effort as explained here : http://java.dzone.com/articles/singleton-design-pattern-%E2%80%93 – Sam Feb 13 '14 at 08:16
  • @Sam [The Java Language Specification](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9.2) says so. It's not actually a block, but `static final` fields with a corresponding initialization expression for each `enum` constant declared (which is pretty much equivalent). – Sotirios Delimanolis Feb 13 '14 at 08:17
  • @SotiriosDelimanolis Please correct me if I am wrong. So making it static final makes an enum thread-safe right? – Sam Feb 13 '14 at 08:29
1

Enum are static instances, they will be initialized in Class loading time. Once they initialized, you can't alter/modify them. So It's thread safe. No modification to Enum instances once they have created.

But, methods that you add to an enum class do not carry any thread safety guarantee unless you declare it explicitly.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105