aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner classes and serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner classes and serialization" Watch "inner classes and serialization" New topic
Author

inner classes and serialization

Ram Manoj
Ranch Hand

Joined: Jan 12, 2008
Posts: 52
Why a class needs to implement Serializable when its serializable inner class is to be serialized?

Here is an example and the runtime exception for it.


java.io.NotSerializableException: manoj.scjp.serialization.Test11
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1075)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1341)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:291)
at manoj.scjp.serialization.Test11.main(Test11.java:11)
Process exited with exit code 0.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1666
    
    7

An inner class contains an implicit reference to the outer class, so for an inner class to be serializable its outer class must be as well.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Mustafa Musaji
Ranch Hand

Joined: May 03, 2008
Posts: 52
Making the inner class static will solve this problem.


SCJP 5.0 - Passed
Ram Manoj
Ranch Hand

Joined: Jan 12, 2008
Posts: 52
Jelle,

So it that an 'outer class' instance is serialized as well with the inner class instance.

Mustafa, thanks for your additional info.
Ram Manoj
Ranch Hand

Joined: Jan 12, 2008
Posts: 52
Anyone please,

Is an 'outer class' instance is serialized as well with the inner class instance when inner class instance is serialized?
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Hello jellem,

An inner class contains an implicit reference to the outer class, so for an inner class to be serializable its outer class must be as well.


Can you please explain me this in simple terms with the help of above example. Please sir...


Thanks<br />Dinesh
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1666
    
    7

From the Serialization specification:


Serialization of inner classes (i.e., nested classes that are not static member classes), including local and anonymous classes, is strongly discouraged for several reasons. Because inner classes declared in non-static contexts contain implicit non-transient references to enclosing class instances, serializing such an inner class instance will result in serialization of its associated outer class instance as well. Synthetic fields generated by javac (or other JavaTM compilers) to implement inner classes are implementation dependent and may vary between compilers; differences in such fields can disrupt compatibility as well as result in conflicting default serialVersionUID values. The names assigned to local and anonymous inner classes are also implementation dependent and may differ between compilers. Since inner classes cannot declare static members other than compile-time constant fields, they cannot use the serialPersistentFields mechanism to designate serializable fields. Finally, because inner classes associated with outer instances do not have zero-argument constructors (constructors of such inner classes implicitly accept the enclosing instance as a prepended parameter), they cannot implement Externalizable. None of the issues listed above, however, apply to static member classes.
Ram Manoj
Ranch Hand

Joined: Jan 12, 2008
Posts: 52
Thanks Jellem,
for the beautiful info regarding Serialization and related to Inner Classes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner classes and serialization
 
Similar Threads
serialization for class Object references
what is printStackTrace()
how FileOutputStream and ObjectOutputStream working here
serialization question
about private void writeObject ( ObjectOutputStream os )