I was asked this question in an interview. The interviewer wanted to know how to make an object immutable. and then he asked what if I serialise this object - will it break immu
You can prevent from serialization or cloning with the help of SecurityManager in java
public final class ImmutableBean {
private final String name;
public ImmutableBean(String name) {
this.name = name;
//this line prevent it form serialization and reflection
System.setSecurityManager(new SecurityManager());
}
public String getName() {
return name;
}
}