Q: List.of(E... elements) specification with regards to underlying array updates

Eirik Bjørsnøs eirbjo at gmail.com
Tue Sep 2 11:46:04 UTC 2025


Hi,

In Venkat's JavaOne 2025  "Know your Java" talk [0], he shows how
Arrays.asList [1] returns a fixed-size List backed by the given array.
Updates to the List are visible to the underlying array and updates to
the array are visible to the List. This mix of fixed-size but
update-friendliness seems clearly documented in the API, but is
perhaps not super intuitive.

Venkat then recommends using List.of(E... elements) [2]  instead,
since this returns an unmodifiable List.

However, while the List returned by List.of is specified to be
unmodifiable, neither the method specification nor the linked
"Unmodifiable lists" section [3] seems to clearly specify whether
updates to the underlying array are visible in the returned list.

One could argue that the word "unmodifiable" implies that updates to
the underlying array are in fact  invisible to the List. However, the
way the specification talks about "unmodifiable lists", I also think a
passable interpretation could be that this is a property of actions
performed on the List implementation, not on the array.

Interestingly, List.copyOf [4] explicitly specifies that modifications
to the given Collection are not reflected in the returned List. (Even
when this may be inferred from the method name!). This prose is what I
feel may be missing from List.of(E... elements).

Should this aspect be clarified, am I missing something in the
existing specification, or should people be expected to not be
difficult like me and instead just read between the lines and infer
this property?

Cheers,
Eirik.


[0] https://www.youtube.com/watch?v=v5Q7TC5u5Co
[1] https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/util/Arrays.html#asList(T...)
[2] https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/util/List.html#of(E...)
[3] https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/util/List.html#unmodifiable
[4] https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/util/List.html#copyOf(java.util.Collection)


More information about the core-libs-dev mailing list