Lesson 1.14: Calling Instance Methods | AP CSA

Unit 1 · Lesson 1.14 · Code Mechanics

Lesson 1.14: Calling Instance Methods

Reading time: 7–9 min·Practice: 8 exercises·Mastery: applied scenario

What you'll learn in this lesson

  • 1.14.A. Call instance methods on objects using the dot operator, and determine the result of those calls.
  • 1.14.A. Explain why calling a method on a null reference throws a NullPointerException.

This lesson is part of the AP CSA Course and Exam Description (effective May 2027).

AP exam weight: Instance method calls appear in every MCQ and FRQ. The AP Java Quick Reference lists instance methods for String that you must know.

Instance methods: called on objects

An instance method is associated with a specific object. It is called using the dot operator on an object reference.

Syntax

objectReference.methodName(arguments)

Examples: str.length(), dog.bark(), account.deposit(100.0)

void vs non-void instance methods

Same rule as class methods

A void instance method performs an action and returns no value — call it as a standalone statement.
A non-void instance method returns a value that must be stored or used in an expression.

ct.increment();              // void: standalone statement
int size = str.length();     // non-void: return value stored

NullPointerException

AP Trap: calling methods on null

A method call on a null reference compiles fine but throws a NullPointerException at run time. The JVM cannot find an object to execute the method on.

String s = null;
System.out.println(s.length()); // NullPointerException
▶ Java Code Editor
Try It Yourself
Write real Java, hit Run, and your code executes on a live compiler. Output is checked automatically.
Tier 2 · AP Practice

Practice Questions

Which of the following correctly calls an instance method on a String?
A. String.length("hello")
B. length("hello")
C. "hello".length()
D. String.length()
C. Instance methods are called on object references using the dot operator: objectRef.method(). "hello" is a String object literal. A treats it like a class method. B has no object. D has no object reference.
A student calls a void instance method inside an expression: int x = ct.increment();. What happens?
A. A compile error because increment() returns void and cannot be assigned to int.
B. Compiles and runs, storing 0 in x.
C. Compiles and runs, storing 1 in x.
D. A run-time error because increment has no return value to store.
A. void methods have no return value. Assigning the result of a void method to a variable is a compile error caught before the program runs.
A Dog variable holds null. What happens when bark() is called?
A. A compile error because you cannot call methods on null.
B. Returns an empty String.
C. Returns null.
D. A NullPointerException is thrown at run time.
D. Calling a method on null compiles fine but fails at run time with NullPointerException. There is no object to execute the method on.
Which of the following is the correct way to use the return value of a non-void instance method?

I. int len = str.length();
II. System.out.println(str.length());
III. str.length();
A. I only
B. I and II only
C. I, II, and III
D. III only
B. I stores the return value in a variable -- correct. II uses it in an expression (as an argument) -- correct. III calls the method but discards the return value. This compiles but is almost always a logic error; the CED says the return value must be stored or used.
Consider the following.

Box b = new Box(10);
b.setSize(25);
System.out.println(b.getSize());

What is printed?
Trace the value of size after each method call.
A. 10
B. 35
C. 25
D. 0
C. Constructor sets size=10. setSize(25) changes size to 25. getSize() returns 25.
Which of the following statements about instance methods is NOT true?

I. Instance methods are called using the dot operator on an object reference.
II. Instance methods can access the attributes of the object they are called on.
III. Instance methods must always return a value.
A. I only
B. I and II only
C. II only
D. III only
D. III is false: void methods do not return a value. I and II are both true statements about instance methods.
Two variables a and b reference the same object. A method is called on a that changes the object's state. What happens when you read the state through b?
A. b still sees the old state because it has its own copy of the object.
B. b sees the new state because both reference the same object.
C. A NullPointerException is thrown when reading through b.
D. b is automatically updated to null after a modifies the object.
B. a and b are aliases -- they point to the same object. Mutations through one are visible through the other. There is only one object in memory.
Which of the following calls is syntactically incorrect?
A. dog.bark
B. dog.bark()
C. System.out.println(dog.bark())
D. String s = dog.bark()
A. Method calls require parentheses. dog.bark (without parentheses) is not a valid method call -- it looks like a field access. B, C, D all have valid syntax.
PRACTICE WITH A GAME — CHOOSE ONE:

Output Predictor — Instance Methods

Predict the exact output. Tap below if you need the class.

Question 1 of 6Score: 0

Bug Hunt — Instance Methods

Each snippet has exactly one buggy line. Click it, then submit.

Bug 1 of 7Caught: 0

Tier 3 · AP Mastery Challenge

The Temperature Sensor

Assume:

class Sensor {
    double reading;
    Sensor(double r) {
        reading = r;
    }
    void calibrate(double offset) {
        reading += offset;
    }
    double getReading() {
        return reading;
    }
    boolean isHigh() {
        return reading > 100.0;
    }
}

Part A

What is printed?

Sensor s = new Sensor(98.5);
s.calibrate(2.0);
System.out.println(s.getReading());
Trace reading after the constructor and after calibrate().
A. 98.5
B. 2.0
C. 100.5
D. 196.5
C. Constructor sets reading=98.5. calibrate(2.0) adds 2.0: reading=100.5. getReading() returns 100.5.

Part B

After the previous code, what does s.isHigh() return?
A. false, because 100.5 is not greater than 100.5.
B. true, because 100.5 > 100.0.
C. A compile error because isHigh() returns boolean not int.
D. NullPointerException because s was calibrated.
B. reading=100.5. 100.5 > 100.0 is true. isHigh() returns true.

Part C: Structured response

Write a statement that creates a second Sensor referencing the same object as s, then explain what would happen if calibrate(-5.0) were called on the second sensor variable and getReading() were then called on s.

Extension · Beyond the Exam

Method chaining

When an instance method returns this (a reference to the current object), calls can be chained: sb.append("hello").append(" world"). This is how StringBuilder works and how many fluent APIs are designed. It is outside AP exam scope for writing, but you may see it in code you read.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]