Junior — Middle
Can you explain what the Serializable interface in Java means and what it is used for?
sobes.tech AI
Answer from AI
In Java, the Serializable interface is used to indicate that an object of a class can be serialized — that is, converted into a byte stream for storage or transmission.
This is used, for example, for:
- Saving the state of an object to disk.
- Transmitting an object over a network.
- Caching objects.
The interface does not contain any methods; it is a marker interface. To make a class serializable, it is enough to implement Serializable:
import java.io.Serializable;
public class User implements Serializable {
private String name;
private int age;
// getters, setters, constructors
}
During serialization, the JVM automatically saves the state of all fields of the object, except those declared as transient.