What would happen if I don't include "implements Serializable?"
public class Student implements Serializable {
private String studentNumber;
private String firstName;
private String lastName;
private ArrayList<Exam> exams;
}
If our class does not implement Serializable interface, or if it is having a reference to a non- Serializable class, then the JVM will throw NotSerializableException . All transient and static fields do not get serialized.
Serializable interface and HashMap also implements the Serializable interface. Then after serializing the HashMap, we will learn how to deserialize the hashmap in Java.
The Serializable interface must be implemented by the class whose object needs to be persisted. The String class and all the wrapper classes implement the java. io. Serializable interface by default.
If an entity instance is to be passed by value as a detached object (e.g., through a remote interface), the entity class must implement the Serializable interface. In practice, if our object is to leave the domain of the JVM, it'll require serialization. Each entity class consists of persistent fields and properties.
Then Student
will behave like a normal class i.e You will not able store the state of the Student
object anywhere
Go through this : https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html
The Student
would not be Serializable, and it will act like a normal class.
Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link. The byte stream can then be deserialized - converted into a replica of the original object.
When you want to serialize an object, that respective class should implement the marker interface serializable. It just informs the compiler that this java class can be serialized.
More
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With