# Check Variable Values

Checking variable values can be a bit tricky in Java.  For example, it can be hard to check the following variable values.&#x20;

```java
class Example 
{
    public static void main(String[] args) 
    {
        String firstName = "Bob";
        String lastName = "Jones";
        System.out.println(firstName);
        System.out.println(lastName);
    }
}
```

This is because `firstName` and `lastName` are local to the `main()` method and created when it is executed and destroyed when it is done.&#x20;

We can prevent this by declaring these variables as static outside the `main()` method.

```java
class Example
{
    static String firstName;
    static String lastName;
 
    public static void main(String[] args)
    {
        firstName = "Bob";
        lastName = "Jones";
        System.out.println(firstName);
        System.out.println(lastName);
    }
}
```

We typically do not teach students to declare variables this way (most curriculums do not) so you may need to tweak your curriculum to auto-grade early assignments that teacher variable assignment and console output.

The grading test involves calling the main method (where you expect the variables to be assigned values and checking that each variable equals the expected value.

```java
import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.*;

public class CodingRoomsUnitTests {

    @Test
    public void testDefaultCase() {
        Example.main(null);
        assertEquals(Example.firstName, "Bob");
        assertEquals(Example.lastName, "Jones");
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://auto-grade.joemazzone.net/java/check-variable-values.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
