AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/1/1-10)
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
intargument to adoubleparameter, soTemps.toCelsius(50)is fine andfholds 50.0. - A non-
voidclass method's return value should be stored or used. - A
voidclass 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.
- Example 1
Calling static methods
Using the
Tempsclass above, what does this code print?System.out.println(Temps.toCelsius(50)); System.out.println(Temps.describe(212.0));Show the solutionHide the solution
- Step 1:
Temps.toCelsius(50): theint50 is widened to thedouble50.0. Then(50.0 - 32)is 18.0,18.0 * 5is 90.0, and90.0 / 9is 10.0. The result is adouble, so it prints10.0. - Step 2:
Temps.describe(212.0): inside,toCelsius(212.0)is called without the class name.(212.0 - 32) * 5 / 9is180.0 * 5 / 9, which is 100.0. - Step 3: The returned string joins left to right:
212.0+F is+100.0+C.
Answer: Two lines:
10.0and212.0F is 100.0C. - Step 1:
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), notnew 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.sqrtis adouble, 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
Check yourself
2 questions on 1.10 Calling Class Methods. Pick an answer to see if you got it, and why.
public class Util
{
public static int twice(int n)
{
return 2 * n;
}
public static int addTwice(int a, int b)
{
return twice(a) + b;
}
}Which of the following statements, appearing in the main method of a different class, compiles and stores 8 in x?
What value is returned by the call Util.addTwice(3, Util.twice(5))?
0 of 2 answered