ACSA Exam Prep
Unit 1 · Using Objects and Methods · Topic 1.10

Calling Class Methods

Skill 2.CSkill 3.CSkill 3.D

Big idea: Class methods (marked static) belong to the class itself, so you call them on the class name and no object is needed.

Learning objectives
  • Develop code to call class methods and determine the result of those calls.

Calling a static method

A class method has static in its header. Call it with the class name and a dot: Math.sqrt(16), Integer.parseInt("42"). Inside the same class, you can drop the class name.

Code
System.out.println(Tools.square(5));
System.out.println(Tools.sumSquares(2, 3));
Output
25
13

Tracing nested calls

Evaluate the innermost call first, replace it with its return value, and work outward.

Code
System.out.println(Tools.square(Tools.square(2) + 1));
Output
25
Key points
  • Class methods have static in the header and are called with ClassName.method(...).
  • In the same class, the class name is optional.
Watch out
  • Calling a static method doesn't need an object, and it can't use instance variables directly (see 3.7).
Quick check
Is Math.abs(-3) a class method call or an instance method call?
Show answer
A class method call: it uses the class name Math, not an object.