Fiveable

💻AP Computer Science A Unit 1 Review

QR code for AP Computer Science A practice questions

1.10 Calling a Non-Void Method

1.10 Calling a Non-Void Method

Written by the Fiveable Content Team • Last updated June 2026
Verified for the 2027 exam
Verified for the 2027 examWritten by the Fiveable Content Team • Last updated June 2026
💻AP Computer Science A
Unit & Topic Study Guides

Frequently Asked Questions

Previous Exam Prep

Study Tools

Exam Skills

AP Cram Sessions 2021

Pep mascot

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 static before 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 Math class is a familiar example: its methods are all class methods, so you write things like Math.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.

</>Java
public 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.

</>Java
public 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:

</>Java
double 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 a Math object.
  • "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 static in the header are class methods. A method without static is an instance method and needs an object.

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.

Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly→ and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot