πŸ““1.10: Static Methods

Table of Contents


πŸ“– This page is a condensed version of CSAwesome Topic 1.10


Calling Static (Class) Methods

Most of the methods we’ve used so far are static methods, also called class methods. These methods are associated with the general class, which also means that there is only one copy of the method.

🎈 A static (class) method is like a school-wide event or rule (e.g., the fire drill procedure). There’s only one copy of the instructions, and it applies to everyone in the school equally.

In contrast: an instance (non-static) method is like a student’s personal schedule (e.g., β€œSarah goes to math class at 10 AM”). Each student has their own individual copy of that schedule, and you’d need to ask the specific student to know what it is. More about these types of methods later!

The main method is always static, as there can only be one copy per Java class.

The main method is like the school’s opening bell: one universal event that always happens the same way to start the day, no matter which students are present.

Template for a Static Method Definition

In the method header, the keyword static is included before the return type:

// static method header
public static returnType methodName(parameters) {
    // method body
}

Non-Void Methods

Up until now, we have used the keyword void as the return type for methods that do not return a value. Think of a void method like giving an instruction without expecting anything back.

Example: If you tell your friend β€œTurn on the light”, they do it, but they don’t hand you anything in return. That’s a void method – it performs an action, but there’s no β€œoutput” to capture.

However, many methods are non-void: think of these kind of like mathematical functions that calculate and return a result, given some arguments.

Example: If you ask your friend β€œWhat’s the temperature outside?”, they give you an answer (a value). That’s a non-void method – it returns something you can use.

⬛️ Void Methods 🎁 Non-Void Methods
DO NOT return a value return a value (output)
β€œJust do an action” β€œDo something AND give me a result”

image

In the next lesson, we will look at the Math library in Java, but consider a simple non-void method that calculates area given length and width:

public static int calcArea(int length, int width) {
    int area = length * width;
    return area;
}

For example, the method call calcArea(2,3) would return 6. What is the data type returned?

➑️ The return statement in a non-void method:

  • Sends a value back to the caller (output).
  • Must return a data type that matches the return type in the method header.
  • Ends method execution immediately.

πŸ“£ When calling non-void methods, you must do something with the return value. You can either store the output in a variable, or use it in an expression directly:

int y = calcArea(3,5); // store return value
System.out.println(y); 
System.out.println(calcArea(4,2)); // use directly

Common Errors with Non-Void Methods

  1. Forgetting to use the return value β€” If a method returns a value, assign it to a variable or use it in an expression.
  2. Mismatched types β€” Ensure the type you store the return value in matches the method’s return type.
  3. Incorrect argument types/order β€” Match the method signature exactly.

Calling Methods from Another Class

In the examples above, we called the methods just by using the method name since they were defined in that same class.

However, if we call a method from a different class, another Java program, we need to include its class name. Class methods are typically called using the class name along with the dot operator (.).

For example, if the calcArea method is in a class called MathFunctions, we could call it as MathFunctions.calcArea().


Summary

  • (AP 1.10.A.1) Class methods are associated with the class (not instances of the class which we will see in later lessons).

  • (AP 1.10.A.2) Class methods include the keyword static in the header before the method name.

  • (AP 1.9.B.1) A void method does not have a return value and is therefore not called as part of an expression.

  • (AP 1.9.B.2) A non-void method returns a value that is the same type as the return type in the header.

  • (AP 1.9.B.2) To use the return value when calling a non-void method, it must be stored in a variable or used as part of an expression.

  • Common errors with methods are mismatches in the order or type of arguments, return values, and forgetting to do something with the value returned from a method. When you call a method that returns a value, you should do something with that value like assigning it to a variable or printing it out.

  • (AP 1.10.A.2) Class methods are typically called using the class name along with the dot operator. When the method call occurs in the defining class, the use of the class name is optional in the call.


Acknowledgement

Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.