1.14 Calling Instance Methods
Call methods on objects using dot notation and use their return values.
Reference Guide
Key Topics
| Term | Definition | Example |
|---|---|---|
| Class | A blueprint that defines fields and methods. | class Cookie { ... } |
| Object | A specific thing made from a class using new. |
Cookie myCookie = new Cookie(); |
| Instance method | A method that acts on one specific object’s fields, called with the dot operator. | myCookie.addChips(3); |
| Static (class) method | A method that belongs to the blueprint, not one object. | Cookie.ovenTempF(); |
| Dot operator | Syntax that tells Java which object’s data a method call should use. | object.method() |
| NullPointerException | Runtime error from calling an instance method on a null reference. |
Dog d = null; d.bark(); |
- Instance methods need an object to run — they read or change that object’s own fields using
this - Static methods belong to the class itself and never need an object
Instance Methods vs. Static/Class Methods
| Feature | Instance method | Static (class) method |
|---|---|---|
| How you call it | myCookie.addChips(2) |
Cookie.ovenTempF() |
Has a this? |
Yes (this = that cookie) |
No — this is illegal |
| Can read/change per-object fields? | Yes | No, unless given an object as a parameter |
| Common uses | Behavior tied to one object’s state | Utilities, factory methods, class-wide info |
Rules for Calling Instance Methods (AP Exam Focus)
- You must have an object reference —
Dog.bark()is invalid unlessbarkis static. - The object reference cannot be
null— calling an instance method on anullreference throws aNullPointerExceptionat runtime. - Arguments must match parameter types exactly, or be compatible through widening conversion, in the same number and order as the method signature.
- A
voidmethod returns nothing —int x = d.bark();is invalid ifbarkisvoid. A non-voidmethod’s return value can be stored or used in an expression. - Chained calls (
obj.method1().method2()) evaluate left-to-right, and each method must return an appropriate object for the next call. - Instance methods can call other instance methods of the same object directly, without needing
this. - Parameter names in the method definition don’t matter when calling — only the argument values and their order/types matter.
Spotting a Required Object
- The call needs one specific object’s data (name, balance, position) → it’s an instance method, call it on an object
- The call is a utility that doesn’t depend on any one object’s state → it’s likely static, call it on the class name
- The reference variable might never have been assigned an object → check for
nullbefore calling an instance method on it
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 dot-operator syntax this lesson teaches is also formally defined by the Java Language Specification’s rules for method invocation expressions (Gosling et al., 2023, §15.12); see References.
Lesson Plan
Learning Objective: By the end of this lesson, you will be able to call instance methods on an object using the dot operator, determine the result of an instance method call, and distinguish an instance method from a static/class method.
Success Criteria: You can look at a code segment calling a method and say whether it compiles, whether it needs an object first, and — if it does — whether a null reference would cause a runtime exception.
Tech Talk & Introduction
- Class = blueprint. A
Cookierecipe says what ingredients exist and what steps to follow. - Object = a thing made from the blueprint. Each cookie baked with
new Cookie(...)is its own entity with its own chocolate chips. - Method = an action. A static/class method belongs to the blueprint, not one cookie (
Cookie.getOvenTemperature()is true for all cookies). An instance method needs one particular cookie to run, because it reads or changes that cookie’s own fields usingthis— you call it on an object, likemyCookie.addChips(3);.
Why do we do this? Every instance method call in Java is really “send this message to this specific object.” The dot operator (object.method()) is what tells Java which object’s data the method should read or change.
sequenceDiagram
participant Main
participant c as Counter
Main->>Counter: new Counter()
activate c
Main->>c: add(5)
c-->>Main: (void)
Main->>c: getCount()
c-->>Main: 5
deactivate c
Code Examples
A. Simple: Syntax of Calling an Instance Method
// Suppose we have a class:
public class Dog {
private String name;
public Dog(String name) {
this.name = name;
}
public void bark() {
System.out.println(name + " says: Woof!");
}
}
// To use it:
Dog d = new Dog("Fido"); // create an instance
d.bark(); // call the instance method on d
Fido says: Woof!
d.bark(); invokes the bark method on the object d. Inside bark, name refers to d’s own name.
B. Intermediate: Passing Arguments to an Instance Method
public class Counter {
private int count;
public void add(int x) {
count += x;
}
public int getCount() {
return count;
}
}
// Use it:
Counter c = new Counter();
c.add(5);
int val = c.getCount(); // returns 5
c.add(5); calls method add on c with argument 5. c.getCount(); calls a non-void method that returns an int.
C. Complex: One Instance Method Calling Another
public class Turtle {
private int x, y;
public void forward(int distance) {
// move the turtle forward by distance
}
public void turnLeft(int degrees) {
// rotate turtle direction
}
public void drawHouse() {
// uses forward and turnLeft to draw a house
forward(50);
turnLeft(90);
forward(50);
// etc.
}
}
Turtle t = new Turtle();
t.drawHouse(); // high-level call, internally calls forward, turnLeft, etc.
Here, drawHouse is an instance method that, when invoked on t, calls other instance methods (forward, turnLeft) on that same object (implicitly this.forward(...), etc.).
D. Bonus: A Class With Two Instance Methods Working Together
// Define the Joke class
class Joke {
private String setup;
private String punchline;
public Joke(String setup, String punchline) {
this.setup = setup;
this.punchline = punchline;
}
public void tellJoke() {
System.out.println(setup);
System.out.println(punchline);
}
}
// Create and run a joke
Joke myJoke = new Joke("Why don't programmers like nature?",
"Because it has too many bugs!");
myJoke.tellJoke();
Hacks & Practice Tasks
Popcorn Hack #1
[!TIP] Add the field first, then write the method, then create the object — in that order.
- Add another property to the dog class for
breed. - Add another method to the dog class that prints “My dog
nameis abreed!” - Create a new object of this class and call the method.
public class Dog {
// ADD CODE HERE
}
// To use it:
// ADD CODE HERE
Popcorn Hack #2
[!TIP] Each new method follows the same pattern as
add: readcount, combine it with the parameter, store the result back incount.
- Add on to the previous counter class and add three more methods for subtraction, multiplication, and division.
- Call all five methods outside of the class.
public class Counter {
private int count;
// ADD CODE HERE
}
// Use it:
// ADD CODE HERE
Homework Hack: MC Practice
For each question, pick an answer and write one sentence citing which Reference Guide rule justifies it.
Question 1
Consider the following class:
public class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
balance = initialBalance;
}
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance;
}
}
Which of the following code segments will compile without error:
A)
BankAccount.deposit(50.0);
double total = BankAccount.getBalance();
B)
BankAccount account = new BankAccount(100.0);
account.deposit(50.0);
double total = account.getBalance();
C)
BankAccount account = new BankAccount(100.0);
double total = account.deposit(50.0);
D)
BankAccount account;
account.deposit(50.0);
double total = account.getBalance();
E)
double total = getBalance();
BankAccount account = new BankAccount(total);
Question 2
What is printed as a result of executing the following code?
public class Rectangle {
private int length;
private int width;
public Rectangle(int l, int w) {
length = l;
width = w;
}
public int getArea() {
return length * width;
}
public void scale(int factor) {
length *= factor;
width *= factor;
}
}
Rectangle rect = new Rectangle(3, 4);
rect.scale(2);
System.out.println(rect.getArea());
A) 12
B) 24
C) 48
D) 96
E) Nothing is printed; the code does not compile
Question 3
Which of the following best describes when a NullPointerException will occur when calling an instance method?
A) When the method is declared as void
B) When the method is called on a reference variable that has not been initialized to an object
C) When the method’s parameters do not match the arguments provided
D) When the method is called from outside the class
E) When the method attempts to return a value but is declared as void
Question 4
Which of the following code segments will NOT compile?
public class Temperature {
private double celsius;
public Temperature(double c) {
celsius = c;
}
public double getFahrenheit() {
return celsius * 9.0 / 5.0 + 32;
}
public void setCelsius(double c) {
celsius = c;
}
}
A)
Temperature temp = new Temperature(0);
System.out.println(temp.getFahrenheit());
B)
Temperature temp = new Temperature(100);
temp.setCelsius(25);
C)
Temperature temp = new Temperature(20);
double f = temp.getFahrenheit();
D)
Temperature temp = new Temperature(15);
int result = temp.setCelsius(30);
E)
Temperature temp = new Temperature(0);
temp.setCelsius(100);
double f = temp.getFahrenheit();
Question 5
Consider the following class:
public class Book {
private String title;
private int pages;
public Book(String t, int p) {
title = t;
pages = p;
}
public String getTitle() {
return title;
}
public int getPages() {
return pages;
}
public void addPages(int additional) {
pages += additional;
}
}
Assume that the following code segment appears in a class other than Book:
Book novel = new Book("Java Basics", 200);
novel.addPages(50);
/* missing code */
Which of the following can replace /* missing code */ so that the value 250 is printed?
A) System.out.println(pages);
B) System.out.println(novel.pages);
C) System.out.println(Book.getPages());
D) System.out.println(novel.getPages());
E) System.out.println(getPages());
References
College Board. (2025). AP Computer Science A: Course and exam description. https://apcentral.collegeboard.org/media/pdf/ap-computer-science-a-course-and-exam-description.pdf
Gosling, J., Joy, B., Steele, G., Bracha, G., & Buckley, A. (2023). The Java language specification: Java SE 21 edition (§15.12, Method Invocation Expressions). Oracle America. https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html