Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[Serialization] why writeObject() must be private?

 
Leon Omk
Ranch Hand
Posts: 75
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I'm wondering why writeObject() must be private?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Seetharaman.

Another question, isn't it wired that private method is invoked outside it's owner class?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic