2

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?

Jyotirup
  • 2,882
  • 9
  • 30
  • 38

1 Answers1

2

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130