1.14 Calling Instance Methods
Call instance methods on objects with the dot operator, use the values non-void methods return, and predict when a call fails to compile or throws a NullPointerException.
1. Reference Guide
| Term | Definition / Example | College Board Requirement |
|---|---|---|
| Instance method | A method that works on one object’s own data, called on that object: myDog.bark(); |
Unit 1, Topic 1.14, p. 48 |
| Dot operator | Connects an object to the method you call on it: object.method() |
Unit 1, Topic 1.14, p. 48 |
| Static (class) method | A method that belongs to the class, called on the class name: Math.sqrt(9) |
Unit 1, Topic 1.10 |
void method |
Does something but returns nothing, so its call is a statement: c.add(5); |
Unit 1, Topic 1.9 |
Non-void method |
Returns a value you can store or use: int n = c.getCount(); |
Unit 1, Topic 1.9 |
NullPointerException |
Run-time error from calling a method on a null reference: Dog d = null; d.bark(); |
Unit 1, Topic 1.14, p. 48 |
- An instance method reads and changes the fields of the object it was called on
- Each object has its own fields, so the same call on two objects can give two different answers
Instance Methods vs. Static Methods
| Feature | Instance method | Static (class) method |
|---|---|---|
| How you call it | myCookie.addChips(2) |
Cookie.ovenTempF() |
| Needs an object? | Yes | No |
| Can use the object’s fields? | Yes | No |
| Common uses | Behavior tied to one object’s state | Utilities and class-wide information |
Rules for Calling Instance Methods (AP Exam Focus)
- You need an object reference:
Dog.bark()does not compile unlessbarkisstatic. - The reference cannot be
null: calling a method onnullthrows aNullPointerExceptionat run time. - Arguments must match the parameters in number, order, and type (or widen to the type).
- A
voidmethod returns nothing:int x = d.bark();does not compile ifbarkisvoid. - A non-
voidmethod’s result can be stored or used in an expression:r1.getArea() + r2.getArea(). - Inside the class, an instance method can call another instance method of the same object without a dot:
forward(50);. - Parameter names in the method header do not matter to the caller. Only the arguments’ values, order, and types do.
Predicting a Call
- The call is on a class name → it compiles only if the method is
static - The variable was never given an object → it does not compile if it is a local variable that was never assigned; it throws a
NullPointerExceptionif it holdsnull - The result is stored in a variable → the method must not be
void, and its return type must fit the variable - Two objects call the same method → each one uses its own fields
College Board Requirements
AP CSA Unit 1, Topic 1.14 Calling Instance Methods. Quoted from the course and exam description (College Board, 2025, p. 48):
- “Instance methods are called on objects of the class. The dot operator is used along with the object name to call instance methods.”
- “A method call on a null reference will result in a NullPointerException.”
The Java language specification defines the dot-operator call as a method invocation expression (Gosling et al., 2023, section 15.12).
Reference: College Board, AP Computer Science A Course and Exam Description (2025), Unit 1, Topic 1.14, p. 48.
2. Real-Life Application
One Order, Many Phones
When you tap “Order” in a food delivery app, the app does not call a method on “orders” in general. It calls methods on your one order object: myOrder.addItem(...), myOrder.getTotal(). Thousands of people order at the same time, and every order keeps its own items and total. Everyone guess: what would go wrong if getTotal() belonged to the class instead of to each order?
| Real Case | What Happened | Java Idea in This Lesson |
|---|---|---|
| Food delivery orders | Every customer’s order keeps its own items and total | Each object has its own fields, so a.getTotal() and b.getTotal() differ |
| Game characters | player.jump() moves one character, not every character |
An instance method acts on the object it is called on |
| A missing account | The app reads a profile that was never loaded and crashes | Calling a method on null throws a NullPointerException |
| A calculator app | Math.sqrt(9) needs no object |
Static methods are called on the class name |
The takeaway: an instance method call says which object to use. Make sure that object exists, then use what the method gives back.
3. Tech Talk
Learning Objective: Call instance methods on objects with the dot operator, use the values non-void methods return, and tell instance methods from static methods.
Success Criteria: You can say whether a call compiles, whether it needs an object first, what it returns, and whether a null reference would make it throw a NullPointerException.
- Class = the recipe. Object = one cookie made from it with
new. - A static method belongs to the recipe:
Cookie.ovenTempF()is the same for every cookie. - An instance method needs one cookie:
myCookie.addChips(3);changes that cookie only.
Counter c = new Counter(); // create the object first
c.add(5); // void: a statement on its own
int value = c.getCount(); // non-void: store what it returns
sequenceDiagram
participant Main
participant c as c (a Counter)
Main->>c: new Counter()
activate c
Main->>c: add(5)
c-->>Main: (void)
Main->>c: getCount()
c-->>Main: 5
deactivate c
Code Examples
A. Calling methods on two objects. The Dog class shows that each object uses its own fields
bark uses name, and each Dog has its own name:
fido.bark(); // Fido says: Woof!
rex.bark(); // Rex says: Woof!
Terminology:
Dot operator: the object before the dot decides whose fields the method uses.
Code Runner Challenge
Predict both lines, run it, then make a third Dog and call bark on it
View IPYNB Source
// CODE_RUNNER: Predict both lines, run it, then make a third Dog and call bark on it
public class Dog {
private String name;
public Dog(String name) {
this.name = name;
}
public void bark() {
System.out.println(name + " says: Woof!");
}
public static void main(String[] args) {
Dog fido = new Dog("Fido");
Dog rex = new Dog("Rex");
fido.bark();
rex.bark();
}
}
Dog.main(null);
B. Arguments and return values. The Counter class mixes void and non-void calls
add changes the object and returns nothing. getCount returns a value, so its call can be stored or printed:
c.add(5); // void
int val = c.getCount(); // non-void: returns an int
int x = c.add(5); // does not compile: add is void
Terminology:
Return value: what a non-void method sends back to the line that called it.
Code Runner Challenge
Predict the three counts, run it, then call add on c2 as well
View IPYNB Source
// CODE_RUNNER: Predict the three counts, run it, then call add on c2 as well
public class Counter {
private int count;
public void add(int x) {
count += x;
}
public int getCount() {
return count;
}
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
c1.add(5);
c1.add(3);
int val = c1.getCount();
System.out.println("c1: " + val);
System.out.println("c2: " + c2.getCount());
System.out.println("Total: " + (c1.getCount() + c2.getCount()));
}
}
Counter.main(null);
Hacks & Practice Tasks
Lesson: CSA 1.14 Calling Instance Methods
MCQ 1.14: <paste the copied result line, such as 3/4 | answers: B,C,A,D>
Popcorn: breed field, describe method, two dogs described with method calls, runs and prints (yes/no); does BreedDog.describe() compile = <your answer>
Homework Hack 1: subtract, multiply, and divide added, both checks print true (yes/no)
Homework Hack 2: compiler error read = <the error line>, all three rectangle checks print true (yes/no)
Homework Hack 3: Student tracker, all four checks print true (yes/no)
Submission Safety Rules (Read First)
- One class per cell, ending with
ClassName.main(null);. - Label the homework hacks 1 through 3.
- Call every instance method on an object, never on the class name.
- Run each Java cell and leave the output showing.
- Include your MCQ score.
- Use
##headings or smaller.
Popcorn Hack (In-Class)
3-minute challenge: give Dog a breed and a method that uses it.
- Add a
breedfield and set it in the constructor - Write a non-
voidmethoddescribe()that returns"My dog <name> is a <breed>!" - In
main, make two dogs and printdescribe()for each one - Answer the question in the Popcorn checker under the runner before you run. When every check passes, copy its line into your submission notes
Code Runner Challenge
Add breed and describe(), describe two dogs, then run it
View IPYNB Source
// CODE_RUNNER: Add breed and describe(), describe two dogs, then run it
// Practice #1 - Try it yourself first!
public class BreedDog {
private String name;
// TODO: add a breed field
public BreedDog(String name) { // TODO: also take and store the breed
this.name = name;
}
// TODO: add describe(), which returns "My dog <name> is a <breed>!"
public static void main(String[] args) {
BreedDog first = new BreedDog("Fido");
// TODO: make a second dog, then print describe() for both dogs
}
}
BreedDog.main(null);
Before you press Run, answer this.
4. MCQ Check
4 questions, one at a time. Answer, check, then go to the next one. At the end, copy the score line into your submission notes.
5. Homework Hack
Task: Do all three hacks. Hack 2 starts with a compiler error on purpose: run it, read the error, then fix it. The other starters compile, so you can run the initial version and compare its incorrect output with the expected output. Keep going until every check prints true.
Optional: The past 1.14 homework has five more practice questions and the full Student Grade Tracker assignment.
Solution Skeleton:
Hack 1: Grow the Counter (5 minutes)
Writing and calling instance methods. Fill in subtract, multiply, and divide. Each one follows the same pattern as add: combine count with the parameter and store the result back in count. The calls in main are already written.
Code Runner Challenge
Fill in subtract, multiply, and divide so both checks print true.
View IPYNB Source
// CODE_RUNNER: Fill in subtract, multiply, and divide so both checks print true.
public class CounterHack {
static class Counter {
private int count;
public void add(int x) {
count += x;
}
// TODO: subtract x from count
public void subtract(int x) {
}
// TODO: multiply count by x
public void multiply(int x) {
}
// TODO: divide count by x
public void divide(int x) {
}
public int getCount() {
return count;
}
}
public static void main(String[] args) {
Counter c = new Counter();
c.add(10);
c.subtract(4);
c.multiply(3);
c.divide(2);
System.out.println("Count: " + c.getCount());
Counter other = new Counter();
other.add(7);
System.out.println(c.getCount() == 9);
System.out.println(other.getCount() == 7);
}
}
CounterHack.main(null);
Hack 2: Void or Not? (5 minutes)
Return values. Run it first: one line tries to store the result of a void method, so it does not compile. Read the error, then fix that line so it calls scale as a statement. Then write getPerimeter, which returns 2 * (length + width), and use the return values of both rectangles in total.
Code Runner Challenge
Run it to see the compiler error, fix the scale line, write getPerimeter, then run until all three checks print true.
View IPYNB Source
// CODE_RUNNER: Run it to see the compiler error, fix the scale line, write getPerimeter, then run until all three checks print true.
public class RectangleHack {
static class Rectangle {
private int length;
private int width;
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
public int getArea() {
return length * width;
}
public void scale(int factor) {
length *= factor;
width *= factor;
}
// TODO: return 2 * (length + width)
public int getPerimeter() {
return 0;
}
}
public static void main(String[] args) {
Rectangle r1 = new Rectangle(3, 4);
Rectangle r2 = new Rectangle(1, 2);
int result = r1.scale(2); // TODO: scale is void, so call it as a statement
int total = 0; // TODO: the area of r1 plus the area of r2, using getArea()
System.out.println("r1 area: " + r1.getArea() + ", perimeter: " + r1.getPerimeter());
System.out.println(r1.getArea() == 48);
System.out.println(r1.getPerimeter() == 28);
System.out.println(total == 50);
}
}
RectangleHack.main(null);
Hack 3: Student Grade Tracker (10 minutes)
Combined. Each Student tracks its own grades. Fill in the four methods:
addGrade(int points)(void): addspointstototalPointsand counts one more assignmentgetAverage()(non-void): returns the average as adouble, or0.0with no assignments. Cast before dividing (Topic 1.5)getLetterGrade()(non-void):"A"for 90 and up,"B"for 80 and up,"C"for 70 and up,"D"for 60 and up, otherwise"F"printReport()(void): prints the name, average, and letter grade
The two students must stay independent: adding a grade to one never changes the other.
Code Runner Challenge
Fill in the four Student methods so all four checks print true.
View IPYNB Source
// CODE_RUNNER: Fill in the four Student methods so all four checks print true.
public class GradeTracker {
static class Student {
private String name;
private int totalPoints;
private int numAssignments;
public Student(String name) {
this.name = name;
}
// TODO: add points to totalPoints and count one more assignment
public void addGrade(int points) {
}
// TODO: return the average as a double, or 0.0 with no assignments
public double getAverage() {
return 0.0;
}
// TODO: return A, B, C, D, or F from the average
public String getLetterGrade() {
return "F";
}
// TODO: print the name, average, and letter grade on one line
public void printReport() {
}
}
public static void main(String[] args) {
Student emma = new Student("Emma");
Student james = new Student("James");
emma.addGrade(95);
emma.addGrade(88);
emma.addGrade(92);
emma.addGrade(85);
james.addGrade(78);
james.addGrade(82);
james.addGrade(75);
emma.printReport();
james.printReport();
System.out.println(emma.getAverage() == 90.0);
System.out.println(emma.getLetterGrade().equals("A"));
System.out.println(Math.abs(james.getAverage() - 78.33) < 0.01);
System.out.println(james.getLetterGrade().equals("C"));
}
}
GradeTracker.main(null);
Grading Plan (1 Point Total)
| Part | Points | What earns the points |
|---|---|---|
| Popcorn | 0.2 | A breed field set by the constructor, a describe() method that returns a String, and two dogs described with calls on objects. The Popcorn checker line is in the notes. |
| MCQ | 0.2 | 4 correct. 0.15 for 2 or 3, 0.1 if every question was answered. |
| Homework Hack 1 | 0.15 | subtract, multiply, and divide change count, and both checks print true. |
| Homework Hack 2 | 0.2 | The compiler error is recorded, scale is called as a statement, getPerimeter and total use return values, and all three checks print true. |
| Homework Hack 3 | 0.25 | All four Student methods work on each object’s own fields, the average is cast before dividing, and all four checks print true. |
| Total | 1.0 |
Quick Validation Checklist
- Each cell ends with
ClassName.main(null);and shows output. - Popcorn checker line in the notes.
- MCQ score in the notes.
- No instance method called on a class name.
- No
voidmethod’s result stored in a variable. - Every homework check prints
true.
6. LxD Cycle Process
Empathize: When students see a class for the first time, they often call its methods on the class name (Dog.bark()), the way they call Math.sqrt(). They have not yet separated what belongs to the class (static) from what belongs to one object (instance), and they try to store the result of void methods.
Define:
- POV: CSA students need one reliable test for when a call needs an object and what it returns, because the AP exam repeatedly shows static and instance calls that look almost the same.
- Learning Goal: Students will call instance methods with the dot operator, use the values non-
voidmethods return, tell instance methods from static methods, and predict when a call fails to compile or throws aNullPointerException.
Ideate:
- HMW Question: How might we give students one question to ask about any call: “which object is this for?”
- HMW Question: How might we make the difference between a
voidand a non-voidcall something students check every time? - Activity: A cookie-recipe analogy, then add methods to Dog and Counter, fix a call that stores a
voidresult, and build a grade tracker with two independent students.
Prototype:
- A reference guide with seven calling rules, runnable two-object and return-value examples, a method-writing Popcorn Hack with a checker, an MCQ knowledge check, three scaffolded homework hacks with self-checking output, and a grading rubric.
- A trial run showed that students who missed the first MC question all called
depositon the class name, so the cookie analogy and the instance-versus-static table come before any questions, and the Popcorn checker asks whether the class-name call compiles. - Excellence means every call is on the right object and every return value is either used or deliberately ignored.
Test:
- Ask peers in peer review to complete the practice without additional explanation.
- Observe whether peers call methods on objects rather than on the class name, and whether they store results only from non-
voidmethods. - Compare your lesson with another that is posted.
- Use the findings to revise any instruction or rubric criterion that did not guide students clearly.
- On submission, collect evidence from runner output, MCQ results, AI grading and student explanations.
- After teaching, grading and analysis, come back and revise lesson to complete teaching cycle for continuous improvement.
7. References/Citations
College Board. (2025). AP Computer Science A course and exam description [Effective fall 2025]. https://apcentral.collegeboard.org/media/pdf/ap-computer-science-a-course-and-exam-description-effective-fall-2025.pdf
Gosling, J., Joy, B., Steele, G., Bracha, G., Buckley, A., Smith, D., & Bierman, G. (2023). The Java® language specification (Java SE 21 ed., section 15.12, Method invocation expressions). Oracle. https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html
Submit Assignment
Need to update a submission later? Open the submissions dashboard.