aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes [Serialization] why writeObject() must be private? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "[Serialization] why writeObject() must be private?" Watch "[Serialization] why writeObject() must be private?" New topic
Author

[Serialization] why writeObject() must be private?

Leon Omk
Ranch Hand

Joined: Aug 17, 2010
Posts: 75



I'm wondering why writeObject() must be private?


OK, so that other guy knows Java better than I do, but I bet he can't speak Wuhanese(a Chinese Dialect) like me.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

ObjectOutputStream uses reflection to find out if those methods are declared and ObjectOutputStream uses getPrivateMethod .so those methods have to be declared private.
Leon Omk
Ranch Hand

Joined: Aug 17, 2010
Posts: 75

Thanks Seetharaman.

Another question, isn't it wired that private method is invoked outside it's owner class?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Leon Omk wrote: isn't it wired that private method is invoked outside it's owner class?

situation matters! serialization technique is performed by JVM , not by other user. so no need for default,protected and public to writeObject method. JVM needs to introspect the class, the solution is reflection.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: [Serialization] why writeObject() must be private?