Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Are classes that implement Serializable required to have no-argument constructors?

0
Posted

Are classes that implement Serializable required to have no-argument constructors?

0

Location: http://www.jguru.com/faq/view.jsp?EID=251942 Created: Nov 12, 2000 Modified: 2000-12-21 16:46:34.173 Author: Tim Rohaly (http://www.jguru.com/guru/viewbio.jsp?EID=10) No. This is a common misconception. The deserialization process does not use the object’s constructor – the object is instantiated without a constructor and initialized using the serialized instance data. The only requirement on the constructor for a class that implements Serializable is that the first non-serializable superclass in its inheritence hierarchy must have a no-argument constructor. (See http://www.jguru.com/jguru/faq/view.jsp?EID=34802 for a more complete explanation). This makes sense: deserialization needs to reconstruct the entire object state, which includes the state of any superclasses. If the superclass is not itself serializable, then deserialization needs to instantiate that superclass from scratch – thus the requirement. For example, with the following class: public class MySerializableCla

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123