Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to serialize model class

Many times I see a model class will implement Serializable, but is never serialized.

  1. What is purpose here to implement Serializable?
  2. If serialization is not used, what will I miss? Is there any effect in the way the code communicates?

public class Stock implements Serializable{

    private int stockId;
    private String stockCode;
    private String stockName;

    //Getter and setter
}
like image 430
suresh Avatar asked May 10 '26 16:05

suresh


1 Answers

What is purpose here to implement Serializable?

Just a marker to indicate the possibility provided to clients of the class for serializing instances of them if they wish.

For example, if you instantiate Stock class and that you want to save Stock instances in a file, you can do it thanks to this marker. APIs (for example Jaxb or Java native serialization mechanism) rely generally on the implementation of this interface to serialize class.

If serialization is not used, what will I miss? Is there any effect in the way the code communicates?

It's is not used, you have zero overhead or transformation in the communication of the instance since it is a marker interface. Only, when the serialization occurs, the communication of the instance changes.

like image 161
davidxxx Avatar answered May 12 '26 08:05

davidxxx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!