Card 05/ 10

ExampleDifficulty: Intermediate1 min

A Method That Adds a Line to Your Order

An order with two lines on it, handed to a method that adds delivery. The caller prints its own list afterwards.

java
static void addDelivery(List<String> lines) {
    lines.add("delivery");
}

List<String> order = new ArrayList<>(List.of("book", "pen"));
addDelivery(order);
System.out.println(order);
text
[book, pen, delivery]

The caller's list has three items. lines and order are two different variables, and both hold the same address, so lines.add(...) followed the address and changed the one list that is there.

The method never touched order. It touched what order points at.