I'm trying to fix a bug in one of my programs which I think might be due to Hibernate figuring out how to instantiate an instance of an object without calling its default (or any other) constructor.
Abstract: De-Serialization creates objects without calling constructors. We can use the same mechanism to create objects at will, without ever calling their constructors. Welcome to the 175th issue of The Java(tm) Specialists' Newsletter.
With serialization, you create a new object by deserializing the whole object state from an input stream. No constructor is invoked in this case. With JNI, there is the AllocObject function, which allocates the space for a new object, also without calling a constructor.
You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors.
A constructor is a special method of a class that initializes new objects or instances of the class. Without a constructor, you can't create instances of the class.
Actually, yes, it is possible to bypass the constructor when you instantiate an object, if you use objenesis to instantiate the object for you. It does bytecode manipulations to achieve this.
Deserializing an object will also bypass the constructor.
It isn't possible to do this using reflection.
Just to complete the picture: using method clone
to create a new object bypasses constructors as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With