1

I know this is a childish question for many but while reading Serializable Objects I am able to understand what is serialization is but not able to identify scenario where to use Serializable Objects.

Points need to be understand

  1. When to implement Serializable interface while creating new class?

    public class ScjpMainClass implements Serializable {
         private static final long serialVersionUID = 4950998071495051590L;
    }
    
  2. What are the benefits we achieved while using Serializable interface in future codes. As i know its saves the sate of objects. but looking a scenario where to use saved state of object in future codes.

Hope you all understand my curiosity about Serializable. May be I am not to much streamlined towards my question but this question strike my mind while studying Serializable

slugmandrew
  • 1,776
  • 2
  • 25
  • 45
Kandy
  • 673
  • 9
  • 21
  • Usually you don't want to serialize as the main outcome is to cause trouble while changing the class. If you want to serialize objects it's usually better to use json, protobuf or something like this. – Nicktar Feb 06 '20 at 12:20
  • very useful link help me lot to understand ...Thanks! https://stackoverflow.com/questions/2475448/need-of-serialization-in-java – Kandy Feb 06 '20 at 12:39

3 Answers3

2

Serializable is used most commonly to:

  1. Send data 'over the wire' - you serialize something into a data stream, then send it across the internet or local network. You can now deserialize it at the other end and retrieve the original data.

  2. Save data to a single field in a data store - something like a NoSQL storage system will often store data in its serialized form, allowing the storage of an almost infinitely complex set of nested objects into a single text field.

There are others but these are the main ones I have come across.

slugmandrew
  • 1,776
  • 2
  • 25
  • 45
1

Implement the Serializable interface when you want to be able to convert an instance of a class into a series of bytes or when you think that a Serializable object might reference an instance of your class.

Serializable classes are useful when you want to persist instances of them or send them over a wire.

Instances of Serializable classes can be easily transmitted. Serialization does have some security consequences, however.

1

You gonna need it, as soon as you are having the need e.g. to store the java class or send it so another program where it is deserialized and used.

Jan Held
  • 634
  • 4
  • 14