1.10 Calling Class Methods
Call static methods on class names and trace nested method calls step-by-step.
- Reference Guide
- College Board Requirements
- Lesson Plan
- Tech Talk
- Code Runner Challenge
- Code Runner Challenge
- Code Runner Challenge
- Code Runner Challenge
- Code Examples
- A. Calling a static method inside its own class
- Code Runner Challenge
- Code Runner Challenge
- B. Nested calls and tracing
- Code Runner Challenge
- C. Static methods and instance variables
- Hacks & Practice Tasks
- References
Reference Guide
A class method belongs to the class, not to any one object. Knowing when a call needs an object (and when it does not) is the key to reading and writing Java classes.
Key Topics
| Term | Definition | Example |
|---|---|---|
| Class method | A method associated with the class, not a specific object. | Calculator.square(5) |
static |
The keyword that makes a method a class method. | public static int square(int n) |
| Instance method | A method that belongs to an object and needs one to run. | p.printStatus() |
| Instance variable | A variable each object has its own copy of. | String name; |
| Static context | Code inside a static method, where there is no current object. |
public static void main(...) |
| Return value | The value a non-void method gives back to the caller. |
return number * number; |
| Nested call | A method call used as an argument to another call. | add(square(2), square(3)) |
Static vs. Instance
| Type | Belongs to | Typical call |
|---|---|---|
| Class / static method | The class | ClassName.method() |
| Instance method | An object | objectName.method() |
Which Call Form?
| Situation | Call form |
|---|---|
| Static method, called from another class | ClassName.method() |
| Static method, called inside its own class | method() or ClassName.method() |
| Instance method | object.method() (an object is required) |
Static method through an object (r.beep()) |
Compiles, but ClassName.method() is clearer |
Common Errors
non-static method ... cannot be referenced from a static context: you called an instance method as if it were static.non-static variable ... cannot be referenced from a static context: a static method tried to use an instance variable directly.- Nested calls run from the inside out: the innermost call finishes first.
College Board Requirements
AP CSA Unit 1, Topic 1.10 Calling Class Methods. From the course and exam description (College Board, 2025, p. 43): class methods are “associated with the class, not instances.”
This lesson covers these Topic 1.10 ideas (paraphrased; check exact wording and numbering in the CED):
- Class methods are associated with the class rather than an object, and their headers include the
statickeyword. - A class method is called with the class name and a dot, as in
ClassName.methodName(). Inside the class that defines it, the class name may be left out. - A class method cannot directly access or change an instance variable, because no specific object is attached to the call. The Java Language Specification describes a static method as invoked without reference to a particular object (Gosling et al., 2025, §8.4.3.2).
- Instance methods need an object, so they are called on an object reference.
- Calls can be traced: a non-
voidmethod returns a value that can be printed, stored, or used in an expression, including as an argument to another call.
Lesson Plan
Learning Objective: Identify class methods, call them correctly, and determine the result of calls to class methods.
Success Criteria: Given a method and a call, you can explain whether it is a class or instance method and whether the call is valid.
Tech Talk
A class method is associated with the class rather than a particular object. In Java, class methods use the static keyword.
Run it, then uncomment the bad call and read the compiler error
public class Printer {
public static void printMessage(String message) {
System.out.println(message);
}
public void printStatus() {
System.out.println("Printer is ready.");
}
public static void main(String[] args) {
Printer.printMessage("Hello!"); // works: static, called on the class
Printer p = new Printer();
p.printStatus(); // works: instance method, called on an object
// Printer.printStatus(); // ERROR: instance method needs an object
}
}
Code Runner Challenge
Run it, then uncomment the bad call and read the compiler error
View IPYNB Source
public static void printMessage(String message) {
System.out.println(message);
}
Because printMessage is static, it can be called using the class name, with no Printer object:
Run it, then add a third call to square with a different number
public class Calculator {
public static int square(int number) {
return number * number;
}
public static void main(String[] args) {
System.out.println(square(5)); // 25, class name omitted
System.out.println(Calculator.square(5)); // 25, class name included
}
}
Code Runner Challenge
Run it, then add a third call to square with a different number
View IPYNB Source
Printer.printMessage("Hello!");
| Type | Belongs to | Typical call |
|---|---|---|
| Class / static method | Class | ClassName.method() |
| Instance method | Object | objectName.method() |
printMessage() is static, so it belongs to Printer. printStatus() is an instance method, so Java needs a particular Printer object.
If you see an error like “non-static method … cannot be referenced from a static context,” check whether you are calling an instance method as if it were static.
Code Runner Challenge
Run it, then uncomment the bad call and read the compiler error.
Run it, trace each line by hand first, then change a number and predict the output
public class NestedCalls {
public static int square(int n) { return n * n; }
public static int add(int a, int b) { return a + b; }
public static void main(String[] args) {
System.out.println(square(3)); // 9
System.out.println(add(square(2), square(3))); // 4 + 9 = 13
System.out.println(square(add(1, 2))); // square(3) = 9
System.out.println(Math.max(square(2), 3)); // max(4, 3) = 4
}
}
Code Runner Challenge
Run it, trace each line by hand first, then change a number and predict the output
View IPYNB Source
// CODE_RUNNER: Run it, then uncomment the bad call and read the compiler error
public class Printer {
public static void printMessage(String message) {
System.out.println(message);
}
public void printStatus() {
System.out.println("Printer is ready.");
}
public static void main(String[] args) {
Printer.printMessage("Hello!"); // works: static, called on the class
Printer p = new Printer();
p.printStatus(); // works: instance method, called on an object
// Printer.printStatus(); // ERROR: instance method needs an object
}
}
Printer.main(null);
Code Examples
A. Calling a static method inside its own class
The Calculator class calls its own static method two ways. When the call is inside the class that defines the method, the class name is optional.
Run it, then uncomment the static printName and read the compiler error
public class Student {
String name;
public Student(String name) {
this.name = name;
}
// ERROR if uncommented: a static method has no specific student
// public static void printName() { System.out.println(name); }
public void printName() { // instance method: uses this student's name
System.out.println(name);
}
public static void welcome() { // static method: uses no instance data
System.out.println("Welcome to class!");
}
public static void main(String[] args) {
Student.welcome();
Student s = new Student("Ari");
s.printName();
}
}
Code Runner Challenge
Run it, then uncomment the static printName and read the compiler error
View IPYNB Source
Calculator.square(5); // with the class name
square(5); // without it (only inside Calculator)
Terminology: Both calls produce 25. From a different class, you must use Calculator.square(5).
Replace every "?" with WORKS or ERROR, then run it and compare
public class Robot {
String name;
public Robot(String name) {
this.name = name;
}
public void introduce() {
System.out.println("I am " + name);
}
public static void beep() {
System.out.println("Beep!");
}
// Predict BEFORE running (Robot r = new Robot("R2"))
static String call1 = "?"; // Robot.beep();
static String call2 = "?"; // r.beep();
static String call3 = "?"; // Robot.introduce();
static String call4 = "?"; // r.introduce();
public static void main(String[] args) {
Robot r = new Robot("R2");
System.out.println("Predictions: " + call1 + ", " + call2 + ", " + call3 + ", " + call4);
Robot.beep();
r.beep();
// Robot.introduce(); // uncomment to see the compiler error
r.introduce();
}
}
Code Runner Challenge
Replace every "?" with WORKS or ERROR, then run it and compare
View IPYNB Source
// CODE_RUNNER: Run it, then add a third call to square with a different number
public class Calculator {
public static int square(int number) {
return number * number;
}
public static void main(String[] args) {
System.out.println(square(5)); // 25, class name omitted
System.out.println(Calculator.square(5)); // 25, class name included
}
}
Calculator.main(null);
B. Nested calls and tracing
The NestedCalls class puts method calls inside other method calls. Java runs them from the inside out, and each call is replaced by its return value.
Trace of add(square(2), square(3)):
square(2)returns 4square(3)returns 9add(4, 9)returns 13
Terminology: Math.max is a real class method from Java’s Math class, so you call it as Math.max(a, b) with no Math object.
Write your corrected calls in main, fill in your explanation, then run it
public class Game {
String player;
public Game(String player) {
this.player = player;
}
public void showPlayer() {
System.out.println("Player: " + player);
}
public static void rules() {
System.out.println("Each player gets one turn.");
}
public static void main(String[] args) {
// ORIGINAL (does not compile):
// Game.rules();
// Game.showPlayer();
// TODO: your corrected calls go here
// My explanation of the original error: ?
}
}
Code Runner Challenge
Write your corrected calls in main, fill in your explanation, then run it
View IPYNB Source
// CODE_RUNNER: Run it, trace each line by hand first, then change a number and predict the output
public class NestedCalls {
public static int square(int n) { return n * n; }
public static int add(int a, int b) { return a + b; }
public static void main(String[] args) {
System.out.println(square(3)); // 9
System.out.println(add(square(2), square(3))); // 4 + 9 = 13
System.out.println(square(add(1, 2))); // square(3) = 9
System.out.println(Math.max(square(2), 3)); // max(4, 3) = 4
}
}
NestedCalls.main(null);
C. Static methods and instance variables
A static method cannot directly use an instance variable. The variable name belongs to an individual Student, and a static method is not connected to one specific student.
public static void printName() {
System.out.println(name); // ERROR: which student's name?
}
Terminology: An instance method can use name, because it runs on a specific object.
// CODE_RUNNER: Run it, then uncomment the static printName and read the compiler error
public class Student {
String name;
public Student(String name) {
this.name = name;
}
// ERROR if uncommented: a static method has no specific student
// public static void printName() { System.out.println(name); }
public void printName() { // instance method: uses this student's name
System.out.println(name);
}
public static void welcome() { // static method: uses no instance data
System.out.println("Welcome to class!");
}
public static void main(String[] args) {
Student.welcome();
Student s = new Student("Ari");
s.printName();
}
}
Student.main(null);
Hacks & Practice Tasks
Popcorn Hack
2-minute challenge: decide whether each call works.
- Write WORKS or ERROR for each of the four calls.
- Run the cell and compare your predictions with the output.
- Uncomment the bad call and read the compiler message.
- For the call that works two ways, note which form is clearer.
Replace every "?" with your own answer.
// CODE_RUNNER: Replace every "?" with WORKS or ERROR, then run it and compare
public class Robot {
String name;
public Robot(String name) {
this.name = name;
}
public void introduce() {
System.out.println("I am " + name);
}
public static void beep() {
System.out.println("Beep!");
}
// Predict BEFORE running (Robot r = new Robot("R2"))
static String call1 = "?"; // Robot.beep();
static String call2 = "?"; // r.beep();
static String call3 = "?"; // Robot.introduce();
static String call4 = "?"; // r.introduce();
public static void main(String[] args) {
Robot r = new Robot("R2");
System.out.println("Predictions: " + call1 + ", " + call2 + ", " + call3 + ", " + call4);
Robot.beep();
r.beep();
// Robot.introduce(); // uncomment to see the compiler error
r.introduce();
}
}
Robot.main(null);
MCQ Check
5 questions, one at a time. Answer, check, then go to the next one. At the end, copy your score line (for example 4/5 | answers: B,C,A,D,C) into your notes.
Question 1 of 5
Which keyword makes a method a class method?
- A.
final - B.
static - C.
void - D.
public
Check answer
**B.** `static` in the method header marks it as a class method. `public` controls access, `void` is a return type, and `final` prevents change.Question 2 of 5
The method public static int square(int n) is in class Calculator. Which call works from a different class?
- A.
Calculator().square(5) - B.
square(5) - C.
Calculator.square(5) - D.
Calculator->square(5)
Check answer
**C.** From another class, call a static method with the class name and a dot. Leaving the class name off (B) only works inside `Calculator`.Question 3 of 5
Printer has a static method printMessage(String) and an instance method printStatus(). Which call does not work?
- A.
Printer.printStatus(); - B.
new Printer().printStatus(); - C.
Printer.printMessage("Hi"); - D.
Printer p = new Printer(); p.printMessage("Hi");
Check answer
**A.** `printStatus()` is an instance method, so it needs an object. D compiles because Java allows static calls through an object, but C is the clearer form.Question 4 of 5
Why does this code fail to compile?
public class Student {
String name;
public static void printName() {
System.out.println(name);
}
}
- A.
nameis never initialized - B.
printlncannot be used in a static method - C.
printNamemust return a value - D. A static method has no specific object, so it cannot directly use the instance variable
name
Check answer
**D.** `name` belongs to an individual `Student`. A static method is not attached to any one student.Question 5 of 5
public static int square(int n) { return n * n; }
// in main:
System.out.println(square(square(2)));
What prints?
- A. 4
- B. 8
- C. 16
- D. 25
Check answer
**C.** The inner call runs first: `square(2)` is 4. Then `square(4)` is 16.Homework Hack
Task: The original program below does not compile. Fix it, then explain the error in one or two sentences.
public static void main(String[] args) {
Game.rules();
Game.showPlayer(); // ERROR
}
Your corrected main should:
- Call
rules()as a class method. - Create a
Gameobject. - Call
showPlayer()on that object. - Run without errors.
// CODE_RUNNER: Write your corrected calls in main, fill in your explanation, then run it
public class Game {
String player;
public Game(String player) {
this.player = player;
}
public void showPlayer() {
System.out.println("Player: " + player);
}
public static void rules() {
System.out.println("Each player gets one turn.");
}
public static void main(String[] args) {
// ORIGINAL (does not compile):
// Game.rules();
// Game.showPlayer();
// TODO: your corrected calls go here
// My explanation of the original error: ?
}
}
Game.main(null);
References
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. (2025). The Java language specification: Java SE 25 edition (Sec. 8.4.3.2). Oracle. https://docs.oracle.com/javase/specs/jls/se25/html/jls-8.html#jls-8.4.3.2