Topic 1.10 — Calling Class Methods

Goal: recognize and write calls to class methods (also called static methods), match calls to method signatures, and store/ use returned values correctly.

The big idea

A class method belongs to the class itself, not to a specific object. You call it using the class name, a dot, and the method name.

Pattern: ClassName.methodName(arguments)

Class method vs. instance method

  • Class method: called with a class name (e.g., Math.random()).
  • Instance method: called with an object/variable (you’ll do this in Topic 1.14).

General syntax

// call a class method
ClassName.methodName(arg1, arg2, ...);

// store a returned value
returnType var = ClassName.methodName(...);

Match the call to the signature

When you call a method, you must match the method’s signature (name + parameter types/order).

// Example signature:
double pow(double a, double b)

// Valid call:
double result = Math.pow(2.0, 3.0);

Return values: use or ignore

If a method returns a value, you can:

  • store it in a variable
  • use it directly in an expression
double r = Math.random();          // store
int n = (int) (Math.random() * 10); // use in expression

void methods can’t be assigned

If a method’s return type is void, it returns nothing—so you cannot store it in a variable.

// Suppose this signature:
void doSomething()

// This is NOT allowed:
int x = doSomething();

Common class methods you’ll see

These are examples of “call with the class name” methods (details of Math come next in 1.11):

Math.random()
Math.abs(...)
Math.pow(...)

The key for 1.10 is the calling pattern, not memorizing everything.

Tracing with class methods

Treat the method call as producing a value, then follow the rest of the code.

double r = Math.random();
int x = (int) (r * 5);

If you’re asked to trace, the question will usually give you enough to reason about the result.

Exam mindset

  • Class methods are called with ClassName. not an object variable.
  • Arguments must match the signature (count, types, order).
  • Return type tells you what can store the result.
  • If it’s void, don’t try to assign it.

Quick self-check

  1. What is the general pattern for calling a class method?
  2. What does the return type tell you?
  3. Why can’t you assign the result of a void method?
  4. What must match a method signature when calling it?
  5. Rewrite a class method call in a sentence (English).

← Back to Unit 1 topics