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
?
Asked
Active
Viewed 69 times
-4

user1803551
- 12,965
- 5
- 47
- 74

Francisco
- 951
- 1
- 6
- 5
-
3Why don't you check the [documentation](https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html)? – shmosel Feb 11 '16 at 07:56
-
Francisco, people answered your question. Then you pulled the rug out from under them by changing the question and invalidating them. That is *not* how we roll here. – Drew Feb 11 '16 at 08:46
-
Question rolled back. Create a new question :P – Drew Feb 11 '16 at 08:48
-
`FileOutputStream` doesn't have `writeObject`. – user1803551 Feb 11 '16 at 08:51
1 Answers
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