1. Using Objects and Methods

Calling Class Methods

Calling Class Methods

students, imagine you are using a vending machine 🤖. You do not build the machine every time you want a snack; you press a button and the machine performs a task for you. In Java, calling a class method is similar: you ask a class to do something by using its method. This lesson will help you understand what class methods are, how to call them, and why they matter in AP Computer Science A.

What a Class Method Is

A method is a named block of code that performs a specific job. A class method is a method that belongs to the class itself instead of a particular object. In Java, class methods are also called static methods.

This means you can use a class method without creating an object first. For example, the method $\text{Math.sqrt}(16)$ returns $4.0$. The class is $\text{Math}$, and $\text{sqrt}$ is the method. You are calling the method directly from the class name.

Class methods are useful for tasks that do not need to remember information about one specific object. Examples include:

  • finding a square root with $\text{Math.sqrt}(x)$
  • getting a random number with $\text{Math.random}()$
  • converting a number to a string with $\text{String.valueOf}(x)$

When you see a class method, think: “This action belongs to the class, not to one particular object.”

How to Call a Class Method

The general format for calling a class method is:

$$\text{ClassName.methodName(arguments)}$$

This includes three important parts:

  • ClassName: the name of the class
  • methodName: the name of the method
  • arguments: values passed into the method, if needed

For example:

$$\text{Math.abs}(-7)$$

This calls the $\text{abs}$ method in the $\text{Math}$ class. The argument is $-7$, and the result is $7$.

Another example:

$$\text{Math.max}(5, 12)$$

This returns $12$, because it compares the two values and gives the larger one.

Some class methods do not take any arguments. For example:

$$\text{Math.random}()$$

This method produces a random decimal number $r$ where $0.0 \le r < 1.0$.

Notice the parentheses $()$. Even if there are no arguments, you still use parentheses when calling a method.

Static Methods vs. Instance Methods

A major idea in AP Computer Science A is the difference between static methods and instance methods.

  • A static method is a class method. It belongs to the class.
  • An instance method belongs to an object made from the class.

Suppose you have a $\text{String}$ object:

$$\text{String word} = \text{"hello"};$$

You can call an instance method on the object:

$$\text{word.length}()$$

Here, $\text{length}$ is an instance method because it works on the specific string $\text{word}$.

But for a class method, you use the class name instead of an object name:

$$\text{Math.pow}(2, 3)$$

This raises $2$ to the $3$rd power and returns $8.0$.

A helpful memory tip is this:

  • If the method depends on one specific object, use the object name.
  • If the method belongs to the whole class, use the class name.

Real-World Examples of Class Methods

Class methods often feel like built-in tools in a calculator or app đź”§.

Example 1: Currency conversion

Suppose a program needs to convert dollars to euros using a fixed exchange rate. The program could use a class method to calculate the new amount without needing a special object for each number.

Example 2: Game scoring

A method like $\text{Math.max}(score1, score2)$ could help a game choose the higher score.

Example 3: Random events

Many games use randomness. A program might use $\text{Math.random}()$ to decide whether a player finds treasure or loses a turn 🎲.

These examples show why class methods are convenient: they provide common operations that many programs need.

Understanding Method Signatures and Return Values

To use class methods correctly, students, you should pay attention to the method signature. A method signature includes the method name and the types of its parameters.

For example, the method $\text{Math.max}(int, int)$ takes two integers and returns an integer. In AP CSA, you often need to know:

  • what values go into the method
  • what type the method returns
  • whether the method changes data or just gives back a result

A return value is the result produced by a method. For example:

$$\text{Math.sqrt}(25) = 5.0$$

The method returns a value that can be used in an expression:

$$x = \text{Math.sqrt}(25) + 2$$

Here, the method call is part of a larger expression, and the returned value is added to $2$.

Some methods are void methods, which means they do not return a value. However, many class methods used in AP CSA return a value. Knowing the return type helps you decide whether you can store the result in a variable or use it directly in a calculation.

Common Mistakes to Avoid

Calling class methods can be simple, but students often make a few common mistakes.

Mistake 1: Using an object name when the method is static

For a class method, write:

$$\text{Math.sqrt}(9)$$

Do not write:

$$\text{m.sqrt}(9)$$

unless $\text{m}$ is part of a different class design where that method is an instance method.

Mistake 2: Forgetting parentheses

This is incorrect:

$$\text{Math.random}$$

This is correct:

$$\text{Math.random}()$$

Mistake 3: Passing the wrong number of arguments

If a method expects two values, like $\text{Math.pow}(a, b)$, then only one value will cause an error.

Mistake 4: Mixing up return values and printed output

If you write:

$$\text{System.out.println}(\text{Math.random}())$$

the method still returns a value, but it is also printed. That is different from just calling the method by itself.

Understanding these mistakes will help you read Java code carefully and write correct programs.

How Calling Class Methods Fits Into Using Objects and Methods

Calling class methods is part of the bigger topic of Using Objects and Methods because it teaches you how to work with Java’s built-in tools and how methods behave.

In AP Computer Science A, you must know how to:

  • create and use objects
  • call instance methods on objects
  • call class methods on classes
  • interpret method return values
  • choose the correct method for the job

For example, if you have a string:

$$\text{String name} = \text{"Alex"};$$

You may call an instance method like:

$$\text{name.substring}(1, 3)$$

This returns $\text{"le"}$.

If you need a general math operation, you may call a class method like:

$$\text{Math.min}(4, 9)$$

This returns $4$.

Both ideas matter because programming is about using the right tool in the right way. Instance methods help you work with a specific object, while class methods help you use a shared operation provided by the class.

Step-by-Step Practice Example

Let’s look at a short code example:

$$\text{double value = Math.sqrt(81);}$$

$$\text{System.out.println(value);}$$

What happens here?

  1. $\text{Math.sqrt}(81)$ is called.
  2. The method returns $9.0$.
  3. The value is stored in the variable $\text{value}$.
  4. The program prints $9.0$.

Another example:

$$\text{int larger = Math.max(14, 20);}$$

The variable $\text{larger}$ gets the value $20$.

This kind of reasoning is important on AP CSA questions. You may be asked to trace a program line by line and predict the output or final variable values.

Conclusion

Calling class methods means using methods that belong to the class itself rather than to a specific object. students, this idea is important because it helps you work with Java’s built-in classes like $\text{Math}$ and understand how methods return values, use arguments, and fit into expressions. When you know whether a method is static or instance-based, you can read code more accurately and write better programs. Class methods are a key part of Java fundamentals and a major piece of using objects and methods in AP Computer Science A ✅

Study Notes

  • A class method is a method that belongs to the class, not to one object.
  • In Java, class methods are usually static methods.
  • Call a class method with the format $\text{ClassName.methodName(arguments)}$.
  • Always use parentheses $()$ when calling a method, even if there are no arguments.
  • Examples of class methods include $\text{Math.sqrt}(x)$, $\text{Math.random}()$, and $\text{Math.max}(a, b)$.
  • A method’s return value is the result it gives back.
  • A static method uses the class name; an instance method uses an object name.
  • Common mistakes include forgetting parentheses, using the wrong name, or giving the wrong number of arguments.
  • Calling class methods is part of the broader AP CSA skill of using objects and methods effectively.
  • Careful tracing of method calls helps you predict program output and understand code behavior.

Practice Quiz

5 questions to test your understanding

Calling Class Methods — AP Computer Science A | A-Warded