Skip to main content

1.10 Calling Class Methods

add to ChatGPT
Verified for the 2027 examUpdated October 2026Fiveable Content Team
Fiveable

💻AP Computer Science A Unit 1 Review

QR code for AP Computer Science A practice questions

1.10 Calling Class Methods

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.

Multiple-choice questions often ask you to decide whether a method call is valid and to predict what it returns, which depends on knowing when the class name is required. Getting comfortable with tracing these calls now pays off in every later unit.

Class Methods Belong to the Class, Not Objects

  • 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 usually 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.

Tracing and Writing Class Method Calls

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 class method that returns a value is usually called for that value, so keep it. For example, writing Math.sqrt(25); on its own line throws away the answer. 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

  • Misconception: You need to create an object to use a class method.
    Actually: You do not. Class methods belong to the class, so Math.random() works without ever making a Math object.
  • Misconception: You must always write the class name.
    Actually: 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.
  • Misconception: Static and instance methods are called the same way.
    Actually: 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.
  • Misconception: The class name in a call is just style.
    Actually: When the call is in a different class, leaving out the class name is an error, not just a style choice.
  • Misconception: Any method can be called as a class method.
    Actually: Only methods declared with static in the header are class methods. A method without static is an instance method and needs an object.

Practice Questions for 1.10

1 of 10

Consider the following class definition:

</>Java
public class Finance {
    public static double getTax(double price) {
        return price * 0.08;
    }
}

Which of the following code segments, appearing in a class other than Finance, correctly calculates the total cost of a $50.0 item by adding the original price to the result of the getTax method?

A.

double total = 50.0 + getTax(Finance, 50.0);

B.

double total = Finance.getTax(50.0 + 50.0);

C.

double total = 50.0 + Finance.getTax(50.0);

D.

double total = 50.0 + Finance.getTax(0.08);

Vocabulary

The following words are mentioned explicitly in the AP® course framework 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.

See Fiveable more often in Google

Mark Fiveable as a preferred source and Google is more likely to show you our guides in Top Stories, with a "preferred" badge in AI Mode and AI Overviews.

Prefer us on Google