How to Create a List in Java with List.of
List.of("editor", "viewer") creates a Java list of the values you give it, in that order, in one line. The list can't be changed afterwards: adding, removing or replacing an element throws an UnsupportedOperationException.
Syntax
List<Type> name = List.of(element1, element2, ...);
| Part | What it does |
|---|---|
List.of(...) | Creates an unmodifiable list of the given elements, in the order given. |
element1, element2, ... | The elements: none, one or as many as you need. None of them can be null. |
Type | The type of the elements, such as String. |
name | The variable's name. |
The List documentation: "The List.of and List.copyOf static factory methods provide a convenient way to create unmodifiable lists." It goes on: "They are unmodifiable. Elements cannot be added, removed, or replaced. Calling any mutator method on the List will always cause UnsupportedOperationException to be thrown."
The Example
The example makes a list of two roles and prints the list, its second element and its size. A changed version then tries to add a third role.
Prerequisites
- A JDK 11 or later, which can run a single
.javafile directly. This guide uses OpenJDK 25.0.5.1.
Step 1: Write the Code
Create a file named Main.java:

import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> roles = List.of("editor", "viewer");
System.out.println(roles);
System.out.println(roles.get(1));
System.out.println(roles.size());
}
}
Step 2: Run It
java Main.java
[editor, viewer]
viewer
2
java Main.java compiles the file in memory and runs it. The command is the same in zsh, bash and PowerShell.
Step 3: Try to Add an Element
Replace Main.java with a version that adds admin to the list:
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> roles = List.of("editor", "viewer");
roles.add("admin");
System.out.println(roles);
}
}
java Main.java
Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:159)
at java.base/java.util.ImmutableCollections$AbstractImmutableCollection.add(ImmutableCollections.java:164)
at Main.main(Main.java:8)

What Happened
System.out.println(roles)printed[editor, viewer], in the order the elements were given.roles.get(1)returnedviewer. Positions start at 0, so 1 is the second element.roles.size()returned 2.roles.add("admin")threwUnsupportedOperationException. The variable is a plainList, which has anaddmethod, so the code compiled. The list itself refused the change when the program ran.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Adding, removing or replacing an element of a List.of list | UnsupportedOperationException, from set as well as add | Copy it into a list that can change: new ArrayList<>(List.of("editor", "viewer")) |
A null among the elements | NullPointerException, thrown from Objects.requireNonNull inside List.of | Leave null out |
get(2) on a list of two elements | IndexOutOfBoundsException: Index: 2 Size: 2 | Use positions from 0 to size() - 1 |
Expecting Arrays.asList to behave the same | It lets set replace an element, but add still throws UnsupportedOperationException | Use List.of for a list that never changes, and an ArrayList for one that does |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
java.lang.UnsupportedOperationException at java.util.ImmutableCollections.uoe | The code changes a list made with List.of | Copy it into an ArrayList first |
java.lang.NullPointerException at java.util.Objects.requireNonNull, with java.util.List.of further down the stack trace | One of the elements is null | Leave the null out, or check the values first |
java.lang.IndexOutOfBoundsException: Index: 2 Size: 2 | The position is past the last element | Check it against size() |
FAQ
Can a List.of list be empty?
Yes. List.of() with nothing between the parentheses makes an empty list, whose size() is 0.
What's the difference between List.of and an ArrayList?
A list from List.of never changes and never holds null. An ArrayList grows and shrinks with add and remove. new ArrayList<>(List.of(...)) starts an ArrayList with the same elements, and adding admin to one printed [editor, viewer, admin].
Can the elements of a List.of list change?
The list can't, but an element that is itself changeable can. The documentation: "However, if the contained elements are themselves mutable, this may cause the List's contents to appear to change." Strings never change, so a list of strings stays exactly as it was made.
Conclusion
List.of makes a list in one line, with its elements in the order you give them. The list can't change and can't hold null, so copy it into an ArrayList when the code needs to change it.
Additional Resources
- Unmodifiable Lists, in the Java 25 API documentation for
List.
