Class methods, also called static methods, belong to the class itself instead of any object, so you call them using the class name with the dot operator, like Math.sqrt(25). You know a method is a class method because it has the keyword static in its header. For AP Computer Science A, trace the arguments, return value, and class name used in each method call.
Why This Matters for the AP Computer Science A Exam
This topic shows up across the exam because so many built-in tools, like the Math class, are made of class methods. On multiple-choice questions you often have to decide whether a method call is valid and predict what it returns, which means recognizing when you need the class name. For free-response code writing, you will call provided methods and need to use the right name and the right arguments. Getting comfortable with tracing these calls now pays off in every later unit.

Key Takeaways
- A class method (static method) is tied to the class, not to any single object, and its header includes the keyword
staticbefore the method name. - The usual way to call a class method is
ClassName.methodName(arguments)using the dot operator. - Inside the class where the method is defined, you can leave out the class name and just call
methodName(arguments). - A non-void class method returns a value, so you store it in a variable or use it in an expression; arguments must match the parameter list in number, order, and type.
- The
Mathclass is a familiar example: its methods are all class methods, so you write things likeMath.abs(-42)without creating any object.
How to Use This on the AP Computer Science A Exam
Code Tracing
When you trace a call to a class method, find the method header, match each argument to its parameter by position, run the body, and bring the returned value back to where the call happened. A class method call interrupts the normal top-to-bottom flow: the program runs the method, then control returns to the spot right after the call.
</>Javapublic class StaticMethodDemo { public static int add(int a, int b) { return a + b; } public static double calculateAverage(double[] numbers) { double sum = 0; for (double num : numbers) { sum += num; } return sum / numbers.length; } public static void main(String[] args) { // Class name optional inside the defining class int sum = add(5, 3); // same as StaticMethodDemo.add(5, 3) // Must use class name for methods from other classes double squareRoot = Math.sqrt(25); // 5.0 double power = Math.pow(2, 8); // 256.0 int absolute = Math.abs(-42); // 42 double[] grades = {90.5, 85.0, 92.3, 88.7}; double avg = calculateAverage(grades); System.out.println("Average: " + avg); } }
Free Response
When the problem gives you a class with class methods, call them with the class name and the dot operator unless you are writing code inside that same class. Since these methods return values, plug the result into a variable or larger expression. Double-check that your arguments line up with the parameter types.
</>Javapublic class Calculator { public static int add(int a, int b) { return a + b; } public static int multiply(int a, int b) { int sum = add(a, b); // class name optional here int sum2 = Calculator.add(a, b); // also valid, just longer return a * b; } } public class MathTest { public static void main(String[] args) { // Outside the defining class, the class name is required int result = Calculator.add(5, 3); // 8 } }
Common Trap
A non-void class method only does something useful if you keep its return value. Writing Math.sqrt(25); on its own line throws away the answer. Store it or use it in an expression instead:
</>Javadouble root = Math.sqrt(25); // keep the value double hypotenuse = Math.sqrt(9 + 16); // use it directly in an expression
Common Misconceptions
- "You need to create an object to use a class method." You do not. Class methods belong to the class, so
Math.random()works without ever making aMathobject. - "You must always write the class name." Outside the defining class you do need it, but inside the class where the method is written, the class name is optional and you can call the method by name alone.
- "Static and instance methods are called the same way." Instance methods are called on an object with the dot operator, while class methods are normally called using the class name with the dot operator.
- "The class name in a call is just style." When the call is in a different class, leaving out the class name is an error, not just a style choice.
- "Any method can be called as a class method." Only methods declared with
staticin the header are class methods. A method withoutstaticis an instance method and needs an object.
Related AP Computer Science A Guides
Vocabulary
The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.Term | Definition |
|---|---|
class methods | Methods that are associated with a class rather than with instances of the class, and include the static keyword in their header. |
dot operator | The symbol (.) used in Java to access instance methods and properties of an object. |
static | A keyword used in a method header to indicate that the method is a class method associated with the class itself, not with individual instances. |
Frequently Asked Questions
What is a class method in AP Computer Science A?
A class method is a method associated with a class rather than a specific object. In Java, class methods include the keyword static in the method header.
How do you call a static class method in Java?
Use the class name, the dot operator, the method name, and the arguments: ClassName.methodName(arguments). For example, Math.sqrt(25) calls the sqrt method from the Math class.
When is the class name optional for a class method call?
The class name is optional when the method call happens inside the same class where the static method is defined. Outside that class, AP CSA expects you to use the class name with the dot operator.
What does a non-void method return?
A non-void method returns a value of the type named in its method header. You should store that value in a variable, use it in an expression, pass it as an argument, or print it.
What is a common mistake when calling non-void methods?
A common mistake is calling a non-void method and ignoring the return value. For example, Math.sqrt(25); computes a value but throws it away unless you store or use the result.
How do you trace a class method call on the AP CSA exam?
Find the method header, match each argument to the correct parameter, run the method body, and replace the call with the returned value. Then continue tracing from the line where the call happened.