Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if I don't implement Serializable when using Hashmap

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;
}
like image 432
Effy Avatar asked Nov 12 '15 14:11

Effy


People also ask

What happens if I don't implement serializable?

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.

Does HashMap implements serializable?

Serializable interface and HashMap also implements the Serializable interface. Then after serializing the HashMap, we will learn how to deserialize the hashmap in Java.

Is it necessary to implement serializable interface?

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.

Is it necessary to implement serializable in entity?

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.


2 Answers

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

like image 101
Rahman Avatar answered Sep 21 '22 06:09

Rahman


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

like image 44
Rakesh KR Avatar answered Sep 18 '22 06:09

Rakesh KR