September 2019
Intermediate to advanced
816 pages
18h 47m
English
Let's now create a list of ImmutableMelon via Collections.unmodifiableList() and the List.of() methods:
private static final ImmutableMelon MELON_1 = new ImmutableMelon("Crenshaw", 2000);private static final ImmutableMelon MELON_2 = new ImmutableMelon("Gac", 1200);private static final List<ImmutableMelon> LIST = Collections.unmodifiableList(Arrays.asList(MELON_1, MELON_2));private static final List<ImmutableMelon> LIST = List.of(MELON_1, MELON_2);
So, is the list unmodifiable or immutable? The answer is immutable. Mutator methods will throw UnsupportedOperationException, and we cannot mutate the instances of ImmutableMelon.
Read now
Unlock full access