Lesson 1.10: Calling Class Methods | AP CSA
Lesson 1.10: Calling Class Methods
What you'll learn in this lesson
-
1.10.A. Identify class methods by the
statickeyword, call them usingClassName.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.
Practice Questions
new.static keyword, associated with the class itself rather than any instance.void.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.result after this statement?double result = Math.pow(2, 8);
(int)(Math.random() * 11) + 10
(int)(Math.random() * 10) + 10
(int)(Math.random() * 20) + 10
(int)(Math.random() * 10) + 11
(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).I.
Math.abs(-8)II.
Math.abs(0)III.
Math.abs(-3.5)
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.double root = sqrt(49);
This code is written outside the
Math class. What is wrong?
sqrt requires a double argument; 49 must be written as 49.0.Math.sqrt(49).int, not a double.sqrt is not a valid Java method name.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.Math.random()?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.Math class methods?I.
Math.pow(3.0, 2.0)II.
new Math().sqrt(9.0)III.
Math.abs(-100)
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.int root = Math.sqrt(64.0);. Which best describes the result?Math.sqrt returns a double and assigning it to int requires an explicit cast.root.root.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.Output Predictor — Math Class Methods
Predict the exact value printed or stored.
Bug Hunt — Class Methods
Each snippet has exactly one buggy line. Click it, then submit.
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
score1?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
score1 = 7. What is the value of multiplier?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.
Scoring Rubric (4 points)
- +1: States what it computes: the absolute difference between the truncated final score and 100 (how far the score is from 100, always non-negative).
- +1: Explains why Math.abs is needed: the subtraction could produce a negative result if finalScore is less than 100; Math.abs ensures a non-negative distance.
- +1: Identifies the (int) issue: truncation rather than rounding means finalScore values like 99.9 become 99, which increases the apparent distance from 100 by 1 compared to rounding.
-
+1: Proposes the fix: use
(int)(finalScore + 0.5)orMath.round(Unit 1 extension) instead of plain cast.
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]