# Tutorial: Set value

From this tutorial, you will learn how to use one of the most basic, but very useful debugger features: Set Value.

While debugging, you get the information about your variables and examine them in order to understand why a program behaves in a particular way. Other times, you would want to reproduce some bug, which depends on some variable. To do that, you would need this variable to hold a particular value.

Reproducing some conditions without modifying the program at runtime may be tedious and time-consuming, so most of the time you would benefit from setting the variable value right from the debugger.

> **Tip:**
> This tutorial assumes that you already know how to suspend an application using the debugger. If you'd like a recap, refer to [Tutorial: Debug your first Java application](debugging-your-first-java-application.html).

## Problem

Let's look at the following simple program:

```JAVA
import java.util.*;

class Aquarium {
    private ArrayList<Fish> fish;

    public static void main(String[] args) {
        var aquarium = new Aquarium();
        System.out.println(aquarium.getFish());
        // fish has already been initialized
        System.out.println(aquarium.getFish()); // line n1
    }

    private ArrayList<Fish> getFish() {
        if (fish == null) initFish();
        return fish;
    }

    private void initFish() {
        fish = new ArrayList<>(Arrays.asList(
                new Fish("Bubbles"),
                new Fish("Calypso"),
                new Fish("Dory")
        ));
    }
}

class Fish {
    private String name;

    Fish(String name) {
        this.name = name;
    }

    public String toString() {
        return name;
    }
}
```

In this code, we have an instance variable `fish` that is printed out twice. It employs lazy initialization, which in our case means that the field is not assigned a value until its getter is called.

Consider the situation when you have already stepped to `line n1`, and `fish` has already been initialized, but you want to look into the `initFish` method? This method will only be executed if `(fish == null)` evaluates to `true`, which is no longer the case at `line n1`.

With this simple program, we could just restart the session. However, in more complex cases, you may find it very inconvenient to relaunch the session and reproduce the steps that lead to a certain state. Let's learn the smarter way.

## Solution

Procedure:

1. According to the problem statement, suspend the program at `line n1`

![The program is suspended at line n1 – the last line of the main() method](https://resources.jetbrains.com.cn/help/img/idea/2026.2/set_value_line_n1.png)

2. Set a breakpoint in the `getFish()` method.

![Breakpoint in the getFish() method](https://resources.jetbrains.com.cn/help/img/idea/2026.2/debug_set_value_breakpoint.png)

3. On the Variables tab, expand `aquarium`, right-click `fish`, and select Set value.

![The 'fish' list on the Variables tab](https://resources.jetbrains.com.cn/help/img/idea/2026.2/debug_set_value_rightclick.png)

4. Enter `null`. Press `Enter` (Windows), `⏎` (macOS), `⏎` (IntelliJ IDEA Classic (macOS)), `⏎` (macOS System Shortcuts), `Enter` (XWin), `Enter` (GNOME), `Enter` (KDE), `Enter` (Emacs), `Enter` (Sublime Text), `⏎` (Sublime Text (macOS)), `Enter` (NetBeans), `Enter` (Visual Studio), `⏎` (Visual Studio (macOS)), `Enter` (Eclipse), `⏎` (Eclipse (macOS)).

![Setting the 'fish' field to null](https://resources.jetbrains.com.cn/help/img/idea/2026.2/debug_set_value_field.png)

> **Tip:**
> This example shows setting the variable to `null`, however you can change it to any value of the correct type. For example, you can use `new ArrayList()` or reference another object of a matching type as long as it is available in the current context.

5. Press `F9` (Windows), `⌘ ⌥ R` (macOS), `F9` (IntelliJ IDEA Classic (macOS)), `⌃ \` (macOS System Shortcuts), `F9` (XWin), `F9` (GNOME), `F9` (KDE), `F9` (Emacs), `F9` (Sublime Text), `F9` (Sublime Text (macOS)), `F5` (NetBeans), `F5` (Visual Studio), `F5` (Visual Studio (macOS)), `F8` (Eclipse), `F8` (Eclipse (macOS)) to resume the debugger session.

Now the condition evaluates to `true`, and we enter the `initFish` method once again, which allows us to see how it performs the initialization.

## Summary

This scenario illustrates how you can modify the variables at runtime to change the flow of your program. Although the example is very simple, you can apply the same principle in more complex projects, where this feature will save you a lot of time.

