Lesson 1.14: Calling Instance Methods | AP CSA
Lesson 1.14: Calling Instance Methods
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
Practice Questions
String?String.length("hello")
length("hello")
"hello".length()
String.length()
int x = ct.increment();. What happens?increment() returns void and cannot be assigned to int.Dog variable holds null. What happens when bark() is called?NullPointerException is thrown at run time.I.
int len = str.length();II.
System.out.println(str.length());III.
str.length();
Box b = new Box(10); b.setSize(25); System.out.println(b.getSize());
What is printed?
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 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?b still sees the old state because it has its own copy of the object.b sees the new state because both reference the same object.NullPointerException is thrown when reading through b.b is automatically updated to null after a modifies the object.dog.bark
dog.bark()
System.out.println(dog.bark())
String s = dog.bark()
Output Predictor — Instance Methods
Predict the exact output. Tap below if you need the class.
Bug Hunt — Instance Methods
Each snippet has exactly one buggy line. Click it, then submit.
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
Sensor s = new Sensor(98.5); s.calibrate(2.0); System.out.println(s.getReading());
Part B
s.isHigh() return?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.
Scoring Rubric (4 points)
-
+1: Correct aliasing statement:
Sensor s2 = s; - +1: States that s2 and s reference the same Sensor object.
- +1: Explains that calibrate(-5.0) on s2 modifies reading: 100.5 + (-5.0) = 95.5.
- +1: States that s.getReading() now returns 95.5 because s and s2 share the same object -- the mutation is visible through both references.
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]