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.
Reviewed against the CED on
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
- Class methods are associated with the class, not with instances of the class.
- Their header includes the keyword static before the method name.
- Call them with the class name and the dot operator: TempConverter.toFahrenheit(100.0).
- 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.
3PrevailLock it in for exam day
By the numbers
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 -
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.