It is obvious that immutability increases the re-usability since it creates new object in each state change.Can somebody tells me a practical scenario where we need a immutable class ?
It is obvious that immutability increases the re-usability since it creates new object in
Share
Consider
java.lang.String. If it weren’t immutable, every time you ever have a string you want to be confident wouldn’t change underneath you, you’d have to create a copy.Another example is collections: it’s nice to be able to accept or return a genuinely immutable collection (e.g. from Guava – not just an immutable view on a mutable collection) and have confidence that it won’t be changed.
Whether those count as “needs” or not, I don’t know – but I wouldn’t want to develop without them.