4

DeSerialization using the Serializable interface mechanism uses Reflection to deserialize the Object bytes to Object. But default no arg constructor is not called during this process and hence its not needed. But all the frameworks like Spring,Hibernate etc need no arg constructor even though they use Reflection. Isn't this contradictory or am I missing something?

asked Apr 22, 2020 at 18:52

1 Answer 1

2

The builtin standard deserialization does not use standard reflection to create instances.

Standard reflection can not create instances without calling a constructor (not necessarily the no arg constructor). So any framework only using standard reflection will need to call a constructor for instance creation.

To create instances without calling a constructor you need some kind of jvm-vendor specific extension, like Unsafe. It is up to the framework developers to use these extensions, many decide against it. (XStream https://x-stream.github.io/faq.html is the only i am aware of that actually uses it)

answered Apr 22, 2020 at 19:29
Sign up to request clarification or add additional context in comments.

8 Comments

On the other hand the super class no-arg constructor is called... you confirm?
All constructors (except on java.lang.Object) will call another constructor (either super class, or one on the same class)
@CodeScale not sure what you mean with "on the other hand". Standard serialization doesnt call any constructors, the instances just comes into existence.
Only talk during deserialization.. not during normal usage of objects creation
@ghostrider not sure what exactly is used. It is a jvm builtin functionality. So it can use anything. But it can not use only standard reflection, because that does not provide enough functionality.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.