Reputation: 2920
How is object of serialized class created dynamically without calling the constructor when de-serialization in java? What is the mechanism used by the JVM to create the object instance?
Upvotes: 2
Views: 244
Reputation: 533680
It depends on the JVM, but the Sun/Oracle/OpenJDK uses sun.misc.Unsafe.allocateInstance(Class)
/** Allocate an instance but do not run any constructor.
Initializes the class if it has not yet been. */
public native Object allocateInstance(Class cls)
throws InstantiationException;
http://www.docjar.com/html/api/sun/misc/Unsafe.java.html
This allows you to create new instances of just about anything e.g. enums, but not new instances of Class.
Upvotes: 2