Suppose you have a class Dog
, that has
public class Dog {
private String name;
private double age;
// some setters
// some getters
Additionally, you have a class DogHandler
, that makes an instance of the Dog d
and passes it to Owner
I suppose, i can
... make a copy of a Dog before passing it to Owner
, but that's an expensive operation and i'd rather avoid it.
... come up with an interface that Dog
implements which contains getters only, cast Dog
to that interface and pass the result along
... initialize settable variables in a constructor and simply not allow changes for this instance of an object
Are there any other ways to make sure receiver of the object cant modify it?
How do you take a simple bean containing some data and make it read-only?