DEV Community

Discussion on: Make an Immutable Object - in Java

Collapse
 
monknomo profile image
Gunnar Gissel • Edited

I wrote this with the mind that users would respect the api and try to break the contract at their own peril.

I'm trying to think of why you'd want your object to be immutable through serialization, and my guess is that you want an immutable singleton? Here's a blog post that covers a bit on dealing with serialization in that case: lingpipe-blog.com/2009/08/10/seria...

As far as reflection goes, you can't get away from it. It's built in. If someone really wants it, they could write some groovy code and ignore access modifiers altogether. Whatever happens after a downstream user brings in reflection to defeat immutability is kind of on them, imho

Collapse
 
ashishmishraw profile image
ashish mishra

Thanks. The blog is useful