Skip to main content

Unit 1 · Topic 1.10

1.10 Calling Class Methods

Some methods belong to a class as a whole rather than to any particular object. These class methods are marked static, and you call them through the class name. The Math methods you'll use all year work this way.

Key terms

  • class method
  • static
  • dot operator

What makes a method a class method

A class method has the keyword static in its header, before the return type: public static double toCelsius(double f). It's tied to the class itself, not to any one object made from the class. That means you don't need to create an object to use it.

Compare this with an instance method like word.length(), which needs a specific String object (see 1.14). A class method has no particular object behind it.

Calling a class method

From outside the class, call a class method with the class name, a dot (the dot operator), the method name and the arguments:

double root = Math.sqrt(16.0); System.out.println(root);

This prints 4.0. Math is the class, sqrt is the class method.

Inside the class that defines the method, you can leave off the class name. In the class below, describe calls toCelsius(f) directly; writing Temps.toCelsius(f) there would also work.

public class Temps { public static double toCelsius(double f) { return (f - 32) * 5 / 9; } public static String describe(double f) { return f + "F is " + toCelsius(f) + "C"; } }

Rules that still apply

Everything from 1.9 still holds for class methods:

  • The arguments must match the parameter list in number, order and type. Java will widen an int argument to a double parameter, so Temps.toCelsius(50) is fine and f holds 50.0.
  • A non-void class method's return value should be stored or used.
  • A void class method can't be used in an expression.
  • The call interrupts the current code, runs the method, then returns to the spot right after the call.

Why use class methods?

Class methods are a good fit for calculations that only depend on their parameters, such as converting units, doing math, or checking a value. Because they don't belong to an object, they can't look at any one object's attributes unless an object is passed in as a parameter.

You've been using a class method since your first program: main is declared public static void main(String[] args). Java runs it without creating any object first, which is only possible because it's static.

Other class methods you'll meet are Integer.parseInt and Double.parseDouble (4.7), which turn a string such as "42" into a number. Spotting static in a method header, or a class name before the dot in a call, tells you that you're dealing with a class method.

You'll write your own static methods in Unit 3 (3.7), and many free-response questions include a helper like this that you're asked to call.

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Calling static methods

    Using the Temps class above, what does this code print?System.out.println(Temps.toCelsius(50)); System.out.println(Temps.describe(212.0));

    Show the solution
    1. Step 1: Temps.toCelsius(50): the int 50 is widened to the double 50.0. Then (50.0 - 32) is 18.0, 18.0 * 5 is 90.0, and 90.0 / 9 is 10.0. The result is a double, so it prints 10.0.
    2. Step 2: Temps.describe(212.0): inside, toCelsius(212.0) is called without the class name. (212.0 - 32) * 5 / 9 is 180.0 * 5 / 9, which is 100.0.
    3. Step 3: The returned string joins left to right: 212.0 + F is + 100.0 + C.

    Answer: Two lines: 10.0 and 212.0F is 100.0C.

Common mistakes

  • Creating an object just to call a static method, or calling a class method on an object variable. Use the class name: Math.sqrt(x), not new Math().
  • Leaving off the class name when calling a static method from a different class. The class name is only optional inside the defining class.
  • Forgetting that the result of a method like Math.sqrt is a double, even when the answer is a whole number.

On the exam

  • Multiple-choice questions may ask which call is valid. Check for static, the class name, and arguments that match the parameter list.
  • In free-response questions, if a class method is provided, call it with the class name (or directly if you're writing code in that same class).

Connected topics

Videos

  • AP Computer Science A - Topic 1.10: Calling Class Methods

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • Calling Class Methods

    CodeHSWatch on YouTube (opens in a new tab)

  • Static vs Non-Static Variables and Methods In Java - Full Simple Tutorial

    Coding with JohnWatch on YouTube (opens in a new tab)

  • Static Methods and Non Static Methods (Java Tutorial)*

    Bill BarnumWatch on YouTube (opens in a new tab)

Check yourself

2 questions on 1.10 Calling Class Methods. Pick an answer to see if you got it, and why.

Code
public class Util
{
    public static int twice(int n)
    {
        return 2 * n;
    }
 
    public static int addTwice(int a, int b)
    {
        return twice(a) + b;
    }
}
Question 1 of 2

Which of the following statements, appearing in the main method of a different class, compiles and stores 8 in x?

Question 2 of 2

What value is returned by the call Util.addTwice(3, Util.twice(5))?

0 of 2 answered