# Initialize a List of Strings in Java Three Ways

> Create a List<String> in Java using ArrayList for mutable lists, Arrays.asList() for fixed-size lists, or List.of() for immutable lists in Java 9+

**URL:** https://sentry.io/answers/how-do-i-initialize-a-list-string-object-in-java/

---

## The Problem

I don't know how to initialize a `List<String>` object in Java.

## The Solution

Depending on your needs and the Java version you're using, there are multiple ways to initialize a `List<String>` object.

We'll demonstrate how to use the following:

- The `ArrayList` class
- The `Arrays.asList()` method
- The `List.of()` method

**Note:** The `List.of()` method can only be used with Java 9+.

### Using `ArrayList`

The most common way to initialize a `List<String>` is to use an `ArrayList`, which is part of the `java.util` package:

```java
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> myList = new ArrayList<>();
        myList.add("Apple");
        myList.add("Banana");
        myList.add("Cherry");

        System.out.println("My List: " + myList);
    }
}
```

In this example, `myList` is an instance of `ArrayList` that holds `String` elements. The `add()` method adds items to the list.

### Using `Arrays.asList()`

Alternatively, you can use `Arrays.asList()` to initialize a `List` with predefined values:

```java
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> myList = Arrays.asList("Apple", "Banana", "Cherry");
        System.out.println("My List: " + myList);
    }
}
```

The `Arrays.asList()` method creates a fixed-size list, meaning you cannot add or remove elements after initialization.

### Using `List.of()`

If you are using Java 9 or later, you can use `List.of()` to create an immutable list:

```java
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> myList = List.of("Apple", "Banana", "Cherry");
        System.out.println("My List: " + myList);
    }
}
```

Lists created with the `List.of()` method are immutable, meaning you cannot add, remove, or modify list elements after initialization.

---

*Source: [sentry.io/answers/how-do-i-initialize-a-list-string-object-in-java/](https://sentry.io/answers/how-do-i-initialize-a-list-string-object-in-java/)*
