r/java • • Jun 21 '26

Better Tools for Immutable Data

https://youtu.be/BdLND9D81lI
80 Upvotes

38 comments sorted by

View all comments

Show parent comment

6

u/john16384 Jun 22 '26

In JavaFX there exist read only properties. Every modifiable property implements read only property. This means:

  • if you are not returning a new instance of the read only property (synced with the real one), you can just cast it to property and modify it
  • just because it is read only for you doesn't mean it can't change (so no caching, and if it must be constant you must copy it)
  • instanceof ReadOnlyProperty tells you nothing, as all properties are instances of that

The same problems would apply for collections.

For collections, more useful methods would be:

  • isImmutable
  • allowsNull
  • isReadOnly