Unit 1 · 10 min

1.10 Calling Class Methods

Some methods belong to the class itself, not to any one object.

Summary, objectives and course and exam description (CED) codes

Class methods are associated with the class, not with instances of the class, and their header has the keyword static before the method name. They are called with the class name and the dot operator, as in TempConverter.toFahrenheit(100.0). Inside the class that defines the method, the class name is optional.

By the end of this session you can

  • 1.10.A Develop code to call class methods and determine the result of those calls.

1.10.A1.10.A.11.10.A.2

Reviewed against the CED on

Know this already? Jump to the checkpoint.

1LearnThe notes, the traps and how to remember them

1. What a class method is

Class methods are associated with the class, not with instances (objects) of the class. Their header includes the keyword static before the method name 1.10.A.1.

Example header: public static double toFahrenheit(double c). The keyword static tells you this is a class method. You can call it without creating any object.

2. Calling a class method from another class

Class methods are typically called using the class name along with the dot operator 1.10.A.2. Here a TempConverter class (written by someone else) has the class method toFahrenheit, and a Weather class uses it:

public class Weather
{
    public static void main(String[] args)
    {
        double hot = TempConverter.toFahrenheit(100.0);
        System.out.println(hot);
        System.out.println(TempConverter.toFahrenheit(30.0));
    }
}

Output:

212.0
86.0

TempConverter.toFahrenheit(100.0) returns 212.0. The return type is double, so the result is stored in a double variable or used in an expression, exactly as in topic 1.9.

In an unrelated class, leaving out the class name does not compile:

// does not compile
public class Weather2
{
    public static void main(String[] args)
    {
        System.out.println(toFahrenheit(100.0));
    }
}

Real view:

Weather2.java:6: error: cannot find symbol
        System.out.println(toFahrenheit(100.0));
                           ^
  symbol:   method toFahrenheit(double)
  location: class Weather2
1 error

3. Calling a class method inside its own class

When the method call occurs in the defining class, the use of the class name is optional 1.10.A.2:

public class TempConverter
{
    public static double toFahrenheit(double c)
    {
        return c * 9 / 5 + 32;
    }

    public static void main(String[] args)
    {
        System.out.println(toFahrenheit(100.0));               // class name left out
        System.out.println(TempConverter.toFahrenheit(0.0));   // class name used
    }
}

Output:

212.0
32.0

Both calls work, because main is inside TempConverter.

Background (not tested): “static method”. Many programmers say static method for what the course calls a class method. They mean the same thing. On the exam, expect “class method”.

Lock it in: memory hooks

  • Class methodsstatic = shared by the whole class: call it by the class's name.
  • Inside vs outsideAt home (in the defining class) you can use the first name only; outside you need the full name: ClassName.method().

Common misconceptions

  • "You must create an object before calling any method."

    Not quite. Class methods belong to the class itself. You call them with the class name, such as Math.sqrt(25.0), without creating an object.

  • "You can always leave out the class name."

    Not quite. Only inside the class that defines the method. In a different class, toFahrenheit(100.0) without TempConverter. does not compile.

Exam traps

  • In an unrelated class, calling toFahrenheit(100.0) without the class name does not compile.
  • Read the header: static means a class method, called with the class name. No static means an instance method, called on an object (topic 1.14).
  • The arguments must still match the signature: TempConverter.toFahrenheit("30") does not compile because a String is not a double.

2PracticeRecall it, then check yourself

In 30 seconds

  1. Class methods are associated with the class, not with instances of the class.
  2. Their header includes the keyword static before the method name.
  3. Call them with the class name and the dot operator: TempConverter.toFahrenheit(100.0).
  4. Inside the class that defines the method, the class name is optional.

Cover this list, say the points out loud, then take the checkpoint without scrolling back up.

Checkpoint · 4 questions

Checkpoint: Calling Class Methods

Pick an answer to mark it. Every answer is explained, and you can try any question again. Your saved score counts first tries only, and stays in this browser.

  1. 1 The TempConverter class contains this method. Which expression, in a different class, correctly calls it? 1.10.A.2
    public static double toFahrenheit(double c)
    Show answer

    Answer: A. Class methods are called with the class name and dot operator, with a double argument. B compiles only inside TempConverter (or a class that inherits from it). C leaves out the required argument. D passes a String, which is not compatible with the double parameter.

  2. 2 The Geometry class contains the following class method. Which statement, in a different class, stores the area of a circle of radius 2.0 in a? 1.10.A.2, 1.10.A.1
    public static double circleArea(double r)
    {
        return 3.0 * r * r;
    }
    Show answer

    Answer: B. Outside the defining class, a class method is called with the class name and the dot operator, and its double result is stored in a double. A leaves out the class name, which only works inside Geometry. C stores a double in an int without a cast. D passes a String instead of a double.

  3. 3 Consider the following class and code segment in a different class. What is printed? 1.10.A.2, 1.10.A.1
    public class Scores
    {
        public static int bonus(int s)
        {
            return s / 10;
        }
    }
    
    // in main
    int total = 47 + Scores.bonus(47);
    System.out.println(total);
    Show answer

    Answer: C. Scores.bonus(47) returns 47 / 10, which is 4 with int division, and 47 + 4 is 51. A ignores the returned value. B uses double division. D prints only the bonus, not the total.

  4. 4 Which of the following headers declares a class method? 1.10.A.1
    Show answer

    Answer: D. A class method has the keyword static before the method name, after the access word and before the return type. A has no static, so it is an instance method. B puts static after the return type, which does not compile. C is a constructor header.

3PrevailLock it in for exam day

By the numbers

Name each list from memory, then reveal it to check. Each list shows how many items to recall.

Key terms: flip the cards

Read the term, say the definition out loud, then flip to check. Struggling a little to remember is what makes it stick.

  • Class methodtap to flip

    A method associated with the class, not with instances of the class. Its header includes the keyword static.

    Example and CED codes
  • Dot operatortap to flip

    The . used to call a method on an object (p.grow(5)) or a class method with the class name (Math.abs(-3)).

    Example and CED codes
  • Methodtap to flip

    A named block of code that only runs when it is called.

    Example and CED codes
  • Classtap to flip

    The formal implementation, or blueprint, of the attributes and behaviors of an object. A class defines a reference type.

    Example and CED codes
  • Method signaturetap to flip

    The method name plus the ordered list of parameter types, such as pow(double, double). With no parameters: the name and an empty list.

    Example and CED codes

Questions students ask

What is a class method in Java?

A class method is a method associated with the class rather than with any one object. Its header includes the keyword static, as in public static double toFahrenheit(double c).

How do you call a static method in Java?

Use the class name, the dot operator and the method name with its arguments, such as Math.sqrt(25.0). Inside the class that defines the method you may leave out the class name.

Is a static method the same as a class method?

Yes. Many programmers say static method; the AP® course says class method. They mean the same thing.

Was this page helpful?

No names are ever shown, and every public comment is checked by us first. Please do not include personal details. To get a reply, email [email protected].

↑ Top

Enlarged view of the selected figure