This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

serializable class  RSS feed

 
anilellendula kumar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Does any one help me on how to Define methods in Serializable interface and how can i use that methods in a class, plz explain me with one example of writing state of an object in the file.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serializable is a 'tag' interface - it has no methods to implement. It is just used to indicate that a class can be serialized (in some cases you specifically DONT want them to be serializable, which is why classes are not serializable by default).

This short example writes out an instance of a Person object to a file on disk. It assumes you have a person class that implements Serializable:

And the code to read it back in again:

Both of these examples could throw an IOException so should be enclosed in try-catch statements.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!