1

From the Serialization documentation

A Serializable class must do the following:

  1. Implement the java.io.Serializable interface.
  2. Identify the fields that should be serializable (Use the serialPersistentFields member to explicitly declare them serializable or use the transient keyword to deno nonserializable fields.)
  3. Have access to the no-arg constructor of its first nonserializable superclass.

What is the premise behind the third point above?

Community
  • 1
  • 1
Geek
  • 26,489
  • 43
  • 149
  • 227

1 Answers1

3

What is the premise behind the third point above?

So that the non-Serializable base classes can be initialized to a known state that can be controlled to some extent by the programmer. It's just a design decision.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • But how are those non-serializable classes getting initialized to a known state? Who is doing the work here? – Geek Sep 09 '13 at 09:44
  • Also can you please explain the *that can be controlled to some extent by the programmer* part of your answer. A code snippet in your answer would be helpful. – Geek Sep 09 '13 at 09:48
  • @Geek *The default no-args constructor of the nearest non-Serializable base class* is doing that. The one you are asking about. The programmer can 'control it to some extent' if he can put code inside it. Surely this is obvious? – user207421 Sep 09 '13 at 09:49
  • But you wrote in a comment to upog's answer that "The default constructor doesn't 'initialize instance variables to their default value'. You can replace it by writing an empty constructor with arguments, and get the same effect"..is the difference here between known state and default state? – Geek Sep 09 '13 at 09:53
  • The point *here* is (a) that there must *be* a default no-args constructor for Serialization to call, and (b) that you can explicitly write your own no-args constructor and have it do anything you like. – user207421 Sep 09 '13 at 09:57