mercredi 21 avril 2021

Immutable class vs. Immutable object [closed]

I have a rather unusual question, which, at least, I could not find anywhere being addressed in this regard.

Here and here, at both places, "Immutable Object" is a phrase Oracle Documentation uses when describing immutability, or immutable objects, to be more precise.

On the other hand, OCA Java SE 8, OCP Java SE 8, Core Java and many(!) other books, references, tutorials and even Oracle's official exam-preparation video courses, use the term "Immutable Class" for the absolutely same idea (of creating immutable objects).

At a glance, this can be thought as a not really important distinction; however, I still think, that there is a semantical difference, which, eventually, leads to the slightly different understanding of immutability.


If we talk about immutable object, then it’s clear.. instance members should be private and final, they're initialized either in-place (next to the declaration part) or via constructors, methods are not overridden and no reference escapes are allowed; however, if we talk about immutable classes, this strikes me that we should also consider static members to be immutable.

It seems to me, that Immutable Class should also have immutable static members, whereas, Immutable Objects should only care about objects, itself.

However, I could not find any clear definition that differentiates these two.

I would really appreciate your valuable thoughts and opinions: am I missing something? am I wrong in my conclusion? is there any clearer definition that I couldn't find so far?

Aucun commentaire:

Enregistrer un commentaire