# AP® Computer Science A 1.14 Calling Instance Methods

Source: https://lockincs.com/ap-csa/unit-1/1-14-calling-instance-methods/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 1.14.A, 1.14.A.1, 1.14.A.2

Instance methods are called on objects of the class, using the object's name and the dot operator, as in p.grow(5). They act on that one object. Calling a method on a null reference throws a NullPointerException.

## What you will learn

- 1.14.A Develop code to call instance methods and determine the result of these calls.

## 1\. Calling instance methods with the dot operator

**Instance methods** are called on objects of the class. The dot operator is used with the object name to call an instance method 1.14.A.1. The method acts on that one object.

This continues the topic 1.13 example: `p` refers to a `Plant` named `"Fern"` with height 30, and `q` refers to a `Plant` named `"Mint"` with height 0.

```
Plant p = new Plant("Fern", 30);
Plant q = new Plant("Mint");
p.grow(5);                         // void: a statement on its own
System.out.println(p.getHeight()); // 35
System.out.println(q.getName() + " " + q.getHeight()); // Mint 0
```

Output:

```
35
Mint 0
```

`p.grow(5)` changed only the object `p` refers to. `q` still has height 0.

An instance method needs an object, so calling it with the class name does not compile:

```
// does not compile
Plant.grow(5);
```

Real view:

```
error: non-static method grow(int) cannot be referenced from a static context
```

## 2\. NullPointerException

A method call on a `null` reference results in a **NullPointerException** 1.14.A.2. The code compiles, but the program stops when the call runs.

```
Plant r = null;                    // r refers to no object
System.out.println("Checking r");
System.out.println(r.getHeight());
System.out.println("Done");
```

Output:

```
Checking r
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Plant.getHeight()" because "r" is null
```

`Done` is never printed: the exception interrupts the normal flow of the program (topic 1.1). The exact wording after the colon can differ between Java versions; the exception name is what matters.

**Watch out:** printing a `null` variable, as in `System.out.println(r);`, is fine and prints `null`. Only calling a method on it throws the exception.

## 3\. Aliases: two names, one object

After `Plant b = a;`, both variables refer to the SAME object. Calling an instance method through either name changes that one object.

```
Plant a = new Plant("Basil", 10);
Plant b = a;
b.grow(5);
a.grow(3);
System.out.println(a.getHeight() + " " + b.getHeight());
```

Output:

```
18 18
```

10 + 5 + 3 is 18, and both names report it, because there is only one `Plant`. (Unit 3 studies aliasing in more depth.)

## 4\. Looking ahead to Unit 3: inside a non-void method

Inside a class, a method’s statements can use the object’s attributes directly, and a `return` statement sends one value back to the caller and ends the method. The value must match the return type in the header. This is what the free-response practice for this unit asks you to write.

```
public class Plant
{
    private String name;
    private int height;

    public Plant(String n, int h)
    {
        name = n;
        height = h;
    }

    // inside the Plant class
    public String getInitial()
    {
        return name.substring(0, 1);
    }
}

// in main
Plant p = new Plant("Fern", 30);
String first = p.getInitial();
System.out.println(first);
```

Output:

```
F
```

If `p` refers to a `Plant` named `"Fern"`, `p.getInitial()` returns `"F"`. A method returns a value; it does not print it. The caller decides what to do with the value.

## Common mix-ups

- Not true: You can call an instance method with the class name, like Plant.grow(5). An instance method needs an object. Plant.grow(5) does not compile; p.grow(5) works, where p refers to a Plant object.
- Not true: Calling a method on null returns null or 0. It throws a NullPointerException at run time and the program stops. The code compiles, because the compiler does not know the variable will be null.
- Not true: After Plant b = a; there are two plants. There is one Plant object and two references to it. A change made through b is seen through a.

## In 30 seconds

- Instance methods are called on objects of the class.
- Use the object's name and the dot operator: p.grow(5).
- Plant.grow(5) does not compile, because an instance method needs an object.
- A method call on a null reference throws a NullPointerException.
- Aliases (two variables referring to the same object) see the same changes.

## Checkpoint: 4 practice questions

### Question 1

**Plant class (implementations not shown)**

| Constructor or method | What it does |
| --- | --- |
| public Plant(String n) | Sets the name to n and the height to 0 |
| public Plant(String n, int h) | Sets the name to n and the height to h |
| public String getName() | Returns the name |
| public int getHeight() | Returns the height |
| public void grow(int cm) | Adds cm to the height |

Consider the following code segment. What is printed?

```java
Plant a = new Plant("Basil", 10);
Plant b = a;
b.grow(5);
a.grow(3);
System.out.println(a.getHeight() + " "
                   + b.getHeight());
```

- A. `13 15`
- B. `13 18`
- C. `18 18`
- D. `10 15`

**Answer: C.** `a` and `b` refer to the same object: 10 + 5 + 3 = 18 for both. A and B treat `b` as a separate copy. D ignores the call `a.grow(3)` and also treats `b` as a copy.

### Question 2

**Plant class (implementations not shown)**

| Constructor or method | What it does |
| --- | --- |
| public Plant(String n) | Sets the name to n and the height to 0 |
| public Plant(String n, int h) | Sets the name to n and the height to h |
| public String getName() | Returns the name |
| public int getHeight() | Returns the height |
| public void grow(int cm) | Adds cm to the height |

What happens when this code segment runs?

```java
Plant s = new Plant("Sage");
s = null;
System.out.println(s.getName());
```

- A. `Sage` is printed.
- B. `null` is printed.
- C. A compile error occurs.
- D. A `NullPointerException` occurs.

**Answer: D.** `s` is null, so calling `getName` on it throws a `NullPointerException` at run time. A is wrong because `s` no longer refers to the Sage object. B would only happen if `s` itself were printed. C is wrong because the compiler does not know `s` will be null.

### Question 3

**Plant class (implementations not shown)**

| Constructor or method | What it does |
| --- | --- |
| public Plant(String n) | Sets the name to n and the height to 0 |
| public Plant(String n, int h) | Sets the name to n and the height to h |
| public String getName() | Returns the name |
| public int getHeight() | Returns the height |
| public void grow(int cm) | Adds cm to the height |

Consider the following code segment. What is printed?

```java
Plant p = new Plant("Rose", 25);
Plant q = new Plant("Lily");
q.grow(p.getHeight());
p.grow(5);
System.out.println(p.getHeight() + " " + q.getHeight());
```

- A. `30 25`
- B. `30 30`
- C. `25 25`
- D. `30 0`

**Answer: A.** `q` starts at 0 and grows by 25 (the value `p.getHeight()` returned at that moment). Then `p` grows to 30. B treats `q` as linked to `p`, but they are separate objects. C forgets `p.grow(5)`. D forgets that `q.grow` used the returned height.

### Question 4

**Plant class (implementations not shown)**

| Constructor or method | What it does |
| --- | --- |
| public Plant(String n) | Sets the name to n and the height to 0 |
| public Plant(String n, int h) | Sets the name to n and the height to h |
| public String getName() | Returns the name |
| public int getHeight() | Returns the height |
| public void grow(int cm) | Adds cm to the height |

Assume `Plant p = new Plant("Fern", 30);` has run. Which of the following statements compiles?

- A. `Plant.grow(5);`
- B. `int h = p.getHeight();`
- C. `int h = p.grow(5) + 30;`
- D. `p.getHeight = 30;`

**Answer: B.** An instance method is called on an object with the dot operator, and `getHeight` returns an int that can be stored. A calls an instance method with the class name. C uses a void method inside an expression. D leaves out the parentheses of the method call and tries to assign to it.

## Questions students ask

**What is an instance method in Java?**

An instance method is a method called on an object of the class. It works with that object's attributes. You call it with the object's name and the dot operator, such as p.getHeight().

**What is the difference between an instance method and a class method?**

An instance method is called on an object (p.grow(5)) and has no static keyword. A class method has static in its header and is called with the class name (Math.sqrt(9.0)).

**What causes a NullPointerException?**

Calling a method on a reference variable that holds null. There is no object to run the method on, so the program throws a NullPointerException at run time.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
