Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Enum singleton are serialization safe?

Internally how serialization / deserialization take place in Enum ? How can jvm generates the same hashcode before ( serialization ) and after (deserialization ) ?

like image 739
user3759177 Avatar asked Jun 05 '15 16:06

user3759177


People also ask

Why enum Singleton is thread safe?

Creation of Enum instance is thread-safe By default, the Enum instance is thread-safe, and you don't need to worry about double-checked locking. In summary, the Singleton pattern is the best way to create Singleton in Java 5 world, given the Serialization and thread-safety guaranteed and with some line of code enum.

Is enum Singleton lazy?

In your case, the singleton will be initialised when the enum class is loaded, i.e. the first time enumClazz is referenced in your code. So it is effectively lazy, unless of course you have a statement somewhere else in your code that uses the enum.

How does serialization Deserialization work with Singleton objects?

Serialization is used to convert an object of byte stream and save in a file or send over a network. Suppose you serialize an object of a singleton class. Then if you de-serialize that object it will create a new instance and hence break the singleton pattern.

What is the advantage of enum in Java?

Java enums are more powerful than C/C++ enums. In Java, we can also add variables, methods, and constructors to it. The main objective of enum is to define our own data types(Enumerated Data Types). Declaration of enum in Java: Enum declaration can be done outside a Class or inside a Class but not inside a Method.


2 Answers

Serialization treats enums specially. Basically, it stores only a reference to its class and the name of the constant. Upon deserialization, this information is used to lookup the existing runtime object of the enum type.

Thus, if you deserialize the enum constant within the same runtime, you will get the same runtime instance you have serialized.

However, when deserializing in another JVM, the hashcode might be different. But having the same hashcode is not a required criteria for singletons. The important point is to never have another instance of the class and this is guaranteed as the serialization implementation will never create an instance of an enum type but only lookup existing constants.

like image 143
Holger Avatar answered Oct 25 '22 08:10

Holger


Internally how serialization / deserialization take place in Enum ?

You can't serialize enums in Java. You can only serialize a reference to an Enum. This means if your enum has any private fields they will not be written nor restored.

How can jvm generates the same hashcode before ( serialization ) and after (deserialization ) ?

Exactly how a JVM generates hashCode is not specified in the JLS and in fact in the source code where is multiple implementations. However, the Oracle 8 update 45 JVM generates the same hashCodes in the same order after restart.

Object[] objs = new Object[5];
for(int i=0;i<objs.length;i++) {
    objs[i] = new Object();
}
RetentionPolicy[] values = RetentionPolicy.values();
System.out.println(objs+": "+objs.hashCode());
for (Object obj : objs) {
    System.out.println(obj+": "+obj.hashCode());
}
for (RetentionPolicy policy : values) {
    System.out.println(policy+": "+policy.hashCode());
}

If you run this with new Object[5] it prints

[Ljava.lang.Object;@677327b6: 1735600054
java.lang.Object@14ae5a5: 21685669
java.lang.Object@7f31245a: 2133927002
java.lang.Object@6d6f6e28: 1836019240
java.lang.Object@135fbaa4: 325040804
java.lang.Object@45ee12a7: 1173230247
SOURCE: 856419764
CLASS: 621009875
RUNTIME: 1265094477

If I reduce the array size to 2 it prints.

[Ljava.lang.Object;@677327b6: 1735600054
java.lang.Object@14ae5a5: 21685669
java.lang.Object@7f31245a: 2133927002
SOURCE: 1836019240
CLASS: 325040804
RUNTIME: 1173230247

Note how it is producing the same hashCodes in the same order. If I comment out the Object[] code completely.

SOURCE: 1735600054
CLASS: 21685669
RUNTIME: 2133927002

The reason you are seeing the same hashCodes each time you run your program is that it is creating the same number of hashCodes before checking the hashCodes of your enums.

Note: an Object doesn't have a system hashCode until you ask for it to reduce the cost of creating objects.

like image 29
Peter Lawrey Avatar answered Oct 25 '22 10:10

Peter Lawrey