π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β |
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 return6
. 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
- Forgetting to use the return value β If a method returns a value, assign it to a variable or use it in an expression.
- Mismatched types β Ensure the type you store the return value in matches the methodβs return type.
- 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 calledMathFunctions
, we could call it asMathFunctions.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.