Lesson 1.10: Calling Class Methods | AP CSA

Unit 1 · Lesson 1.10 · Code Mechanics

Lesson 1.10: Calling Class Methods

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

What you'll learn in this lesson

  • 1.10.A. Identify class methods by the static keyword, call them using ClassName.methodName(), and determine the result of those calls.
  • 1.10.A. Recognize when the class name can be omitted (within the defining class) and when it is required.

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

AP exam weight: Class methods appear in nearly every MCQ and FRQ. The Math class methods are on the Java Quick Reference provided during the exam — you must know what they do and how to call them correctly.

Key Vocabulary

Term Definition
class method A method declared with the static keyword; associated with the class itself, not any object instance. Called as ClassName.method().
static A keyword indicating a class-level method or field; does not require an object instance to use.
Math.abs(x) Returns the absolute value of x. Overloaded for int (returns int) and double (returns double).
Math.pow(base, exp) Returns base raised to the power exp as a double. Always returns a double.
Math.sqrt(x) Returns the non-negative square root of x as a double. Always returns a double.
Math.random() Returns a double value in the range [0.0, 1.0); 0.0 can be returned, 1.0 cannot.
random range formula (int)(Math.random() * range) + min produces a random int from min to min+range-1 inclusive.

What is a class method?

A class method (also called a static method, defined in the Java Language Specification §8.4.3.2) is associated with the class itself, not with any particular object instance. The keyword static appears in the method header.

How to Call a Class Method

ClassName.methodName(arguments)

Examples: Math.abs(-5), Math.sqrt(16.0), Math.random()
When calling a class method from within the same class, the class name is optional.

AP Trap: static vs instance

Class methods do not require an object. You never write new Math() to use Math methods. You call them directly on the class name: Math.abs(x). Calling a class method on an object reference (like m.abs(x) where m is a Math variable) would be unusual and is not the expected pattern on the AP exam.

The Math class methods (Quick Reference)

These five Math methods appear on the AP exam Java Quick Reference. You must know all of them:

AP Quick Reference: Math Class

static int abs(int x) — returns the absolute value of an int
static double abs(double x) — returns the absolute value of a double
static double pow(double base, double exponent) — returns baseexponent
static double sqrt(double x) — returns the non-negative square root
static double random() — returns a double in [0.0, 1.0) — 0.0 is possible; 1.0 is never returned

Using Math.random() to generate ranges

Because Math.random() returns a value in [0.0, 1.0), you scale and shift to produce a random int in any range:

Formula

(int)(Math.random() * range) + min

Produces a random int from min to min + range - 1 (inclusive).
Example: random 1–6 die: (int)(Math.random() * 6) + 1

AP Trap: Math.random() range

Math.random() is exclusive of 1.0. (int)(Math.random() * 6) produces 0–5, not 0–6. Students who multiply by n instead of n+1 or forget to add the minimum consistently lose points on random-range questions.

▶ 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 identifies a class (static) method?
A. A method that can only be called on an object created with new.
B. A method declared with the static keyword, associated with the class itself rather than any instance.
C. A method that does not take any parameters.
D. A method whose return type is void.
B. The static keyword in the method header marks it as a class method. It is called on the class name, not on an object. A describes instance methods. C and D describe unrelated characteristics.
What is the value of result after this statement?

double result = Math.pow(2, 8);
Compute 2 raised to the 8th power before reading the options.
A. 16.0
B. 28.0
C. 16
D. 256.0
D. Math.pow(2, 8) = 28 = 256. Returns a double: 256.0. A is 24. B adds instead of exponentiating. C is wrong type (Math.pow always returns double).
Which expression produces a random integer from 10 to 20 inclusive?
A. (int)(Math.random() * 11) + 10
B. (int)(Math.random() * 10) + 10
C. (int)(Math.random() * 20) + 10
D. (int)(Math.random() * 10) + 11
A. Range size = 20 - 10 + 1 = 11. Formula: (int)(Math.random() * 11) + 10 produces 10–20. B produces 10–19 (misses 20). C produces 10–29 (too wide). D produces 11–20 (misses 10).
Which of the following expressions evaluate to a positive result?

I. Math.abs(-8)
II. Math.abs(0)
III. Math.abs(-3.5)
A. I only
B. I and II only
C. I and III only
D. I, II, and III
C. I: Math.abs(-8) = 8 — positive. II: Math.abs(0) = 0 — zero, not positive. III: Math.abs(-3.5) = 3.5 — positive. I and III return positive values; II returns zero.
A student writes the following to compute the square root of 49.

double root = sqrt(49);

This code is written outside the Math class. What is wrong?
A. sqrt requires a double argument; 49 must be written as 49.0.
B. The class name is missing; it must be written as Math.sqrt(49).
C. The return value must be stored in an int, not a double.
D. sqrt is not a valid Java method name.
B. Class methods called from outside the defining class require the class name with the dot operator: Math.sqrt(49). A is wrong: int 49 widens to 49.0 automatically. C is wrong: double is the correct return type. D is wrong: sqrt is a valid and documented Math method.
Which of the following values can NEVER be returned by Math.random()?
A. 0.0
B. 0.5
C. 0.999999
D. 1.0
D. Math.random() returns a value in the range [0.0, 1.0) — 0.0 is included (can be returned), but 1.0 is excluded (never returned). 0.5 and 0.999999 are both in the valid range.
Which of the following are valid calls to Math class methods?

I. Math.pow(3.0, 2.0)
II. new Math().sqrt(9.0)
III. Math.abs(-100)
A. I only
B. II only
C. I and III only
D. I and II only
C. I and III use the correct class-name dot-method syntax for static methods. II attempts to instantiate Math with new, which would cause a compile error because Math has a private constructor and is not meant to be instantiated. Static methods are called on the class, not on objects.
A student writes int root = Math.sqrt(64.0);. Which best describes the result?
A. A compile error because Math.sqrt returns a double and assigning it to int requires an explicit cast.
B. Compiles and runs, storing 8 in root.
C. A run-time error because 64.0 is not a perfect square.
D. Compiles and runs, storing 8.0 in root.
A. Math.sqrt always returns a double. Assigning a double to an int variable without an explicit cast is a narrowing conversion — compile error. Fix: int root = (int) Math.sqrt(64.0);. D is wrong because int cannot hold 8.0.
PRACTICE WITH A GAME — CHOOSE ONE:

Output Predictor — Math Class Methods

Predict the exact value printed or stored.

Question 1 of 6Score: 0

        

Bug Hunt — Class Methods

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

Bug 1 of 7Caught: 0

Tier 3 · AP Mastery Challenge

The Game Randomizer

A student writes a game that needs to generate random scores. Review the code and answer the questions.

int score1 = (int)(Math.random() * 100);
int score2 = (int)(Math.random() * 100) + 1;
double multiplier = Math.pow(2, score1 % 5);
double finalScore = score1 * multiplier;
System.out.println(Math.abs((int) finalScore - 100));

Part A

What is the possible range of values for score1?
A. 1 to 100 inclusive
B. 0 to 100 inclusive
C. 0 to 99 inclusive
D. 1 to 99 inclusive
C. Math.random() * 100 produces [0.0, 100.0). (int) truncates, giving 0–99. 100 is never produced because Math.random() never returns 1.0.

Part B

Assume score1 = 7. What is the value of multiplier?
Compute score1 % 5 first, then raise 2 to that power.
A. 4.0
B. 4.0... wait: 7%5=2, 2^2=4. Correct: 4.0
C. 128.0
D. 2.0
B. score1 % 5 = 7 % 5 = 2. Then Math.pow(2, 2) = 4.0. Multiplier = 4.0.

Part C: Structured response

Explain what Math.abs((int) finalScore - 100) computes and why Math.abs is needed. Then identify one potential issue with using (int) finalScore rather than rounding.

Extension · Beyond the Exam

Why all Math methods are static

The Math class has a private constructor, so you can never create a Math object. All its methods are utility functions that depend only on their arguments, not on any stored state. Making them static means you call them directly without creating an instance first. This is a common design pattern for utility classes in Java.

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]