-4

If class B extends class A, class B implements Serializable, and class A has a public static initialized variable that is not serializable... trying to write a class' B object with writeObject() method of FileOutputStream, will serialize the non-serializable inherited member from A for writing it in the file with the rest of the variables of class B or it will throw NotSerializableException?

user1803551
  • 12,965
  • 5
  • 47
  • 74
Francisco
  • 951
  • 1
  • 6
  • 5

1 Answers1

1

This works because static fields aren't saved when you write your object over an output stream.

From the documentation:

The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields. References to other objects (except in transient or static fields) cause those objects to be written also. Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written.

Ferrybig
  • 18,194
  • 6
  • 57
  • 79