An example of the internals of a immutable class being shared in other related classes in core Java [closed]

StackOverflow https://stackoverflow.com/questions/11732615

Question

One of the many advantages of an immutable class is that their internals can actually be shared somehow in other related classes.

What can be cited as an example of this technique from core java library ? Why does this technique work ?

Edit : This was actually asked in an Oracle interview to me .

Was it helpful?

Solution

One of the many advantages of an immutable class is that their internals can actually be reused somehow in other related classes.

I don't buy that argument / premise:

  • Java doesn't allow you to reuse the internals of a class ... unless you count copy-and-paste programming a legitimate reuse strategy. In Java, you reuse the entire class, or none of it.

  • You can reuse mutable classes in the same way as immutable classes.

  • If you need the entities to be mutable, the immutability in the original class cab actually be an impediment to reuse.


This premise is actually written out in Effective Java as mentioned in this answer that got 121 up votes.

Big deal! Just because you quoted (without any context) from a well known book doesn't make your premise credible. Lots of well known books say things that are debatable or even plain wrong.

Just because 121 people voted for an Answer that quoted this one line (among a number of others) doesn't add to the credibility. They were voting for the entire answer, not necessarily that sentence. Besides, votes don't indicate objective correctness. They indicate subjective agreement.

If this was a debating / discussion forum, you should counter my arguments with proper arguments of your own. But it is not, and this Question has (IMO rightly) been closed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top