Static Methods and Their Definitions
A static method is called from another class by writing the class name, a dot, and the method name: ClassName.methodName();
A Call That Looks Reasonable
Programs are usually split across several classes, and a method written in one class is often useful in another. Imagine a class named Poker that contains a public static method called playGame(). Because the method is static, it belongs to the class itself rather than to any particular object created from it. Now suppose a second class, Main, wants to run playGame(). Main is not Poker, so the question becomes: how must the call be written so that Java finds the right method?
What do you think happens?
Main is a different class from Poker. If Main simply writes playGame(); with no class name in front of it, what happens?
Reveal answer
It fails to compile
A bare call such as playGame(); works only inside the class that defines the method. From another class the bare name does not tell the computer which class to search, so the statement fails to compile. The fix is to name the class in front of the method.
Tracing Which Class Owns Each playGame
A static method's full name includes the name of the class where it is defined. The name playGame() on its own is only part of the story. The source describes three classes that each define a method with this same name: Poker, Roulette, and Blackjack. Each one has a playGame() method, but each belongs to a different class. The class name is the qualifier that separates them, so the same method name can be reused in different classes without confusion about which one is meant.
Read the diagram column by column. The three qualified calls all share the method name, but each names a different class, so each points to a different method. The bare call has no qualifier at all. Because it does not say which class to search, it cannot select one of these methods from outside the class that defines it.
Writing the Qualified Call
A static method is called from another class by writing the class name, a dot, and the method name, in the form ClassName.methodName();. The class name tells the computer which class to search for the method, and the dot connects the class name to the method name. Together they form a complete reference to one specific method.
- The method is public, so other classes are allowed to call it
- The static keyword means the method belongs to the class itself, not to an object
- The full name of this method includes the class name Poker
public class Main {
public static void main(String[] args) {
Poker.playGame();
}
}- 1The public class Main contains the main method, which is where execution begins
- 2The statement
Poker.playGame(); has the class name Poker, a dot, and the method nameplayGame - 3The class name tells the computer to search Poker for
playGame, so the method in Poker runs - 4The output shown follows from the body of
playGame()in the Poker class above
- Main is a different class from Poker
- The qualified call
Poker.playGame()names the class where the method is defined
Playing pokerWhy the Bare Call Fails
The statement playGame(); can appear inside the class that contains the definition, such as within main() or another subroutine of that class. Outside that class, the bare name does not tell the computer where to look. This is the cause of the compile-time failure: the statement reaches the compiler without the information it needs to pick a class, so the program does not compile.
How a Static Method Sits Inside Its Class
A static method is contained within the class that defines it. Poker is the container, and playGame() is the method inside it. When Main writes Poker.playGame(), the class name is the pointer that leads into that container and selects the method defined there. The diagram below shows the containment and the call that points into it.
Calling Two Classes That Share a Method Name
Calling playGame in Poker and Roulette
Write a Main class that runs the playGame() method from both Poker and Roulette, where both classes define a public static playGame() method.
- 1Identify the two methods
Both classes define a method with the same name, so the bare name
playGame()would be ambiguous from Main. Each call must name its class. - 2Write the first qualified call
Poker.playGame(); has the class name Poker, a dot, and the method name. It selects the method defined in Poker. - 3Write the second qualified call
Roulette.playGame(); uses the same method name but the qualifier Roulette, so it selects the method defined in Roulette. - 4Check for a bare call
No statement in Main is written as
playGame(); alone. Every call names the class, so the calls are complete references to specific methods.
Main runs Poker's playGame() and then Roulette's playGame(). The shared method name causes no conflict because each call includes its class name.
Mistakes Beginners Make with Static Calls
Common mistake Writing a bare call to a static method from another class
What it looks likepublic class Main { public static void main(String[] args) { playGame(); } }Why it failsThe bare name
playGame()does not tell the computer which class to search. Because Main is not Poker, the call fails to compile.Do this insteadWrite Poker.playGame(); so the class name tells the computer where to find the method.
Common mistake Dropping the dot or putting the class name in the wrong place
Why it failsThe form
ClassName.methodName()depends on the dot connecting the class name to the method name. Without the correct structure, the reference does not point to one specific method.Do this insteadKeep the order exactly as ClassName, a dot, then methodName().
Common mistake Assuming the bare call works because the method exists
Why it failsThe method existing in Poker is not enough. The call itself must give the class name when it is made from outside that class.
Do this insteadCheck whether the call is inside the defining class. If it is not, include the class name.
Practice: Fixing a Call from Another Class
A class named Blackjack defines a public static method playGame(). A class named Main, which is a different class, contains the statement playGame(); inside its main method. Predict whether this compiles. Then rewrite the statement so it correctly calls the Blackjack version, and explain which part of the call identifies the class.
Hints
- Ask whether the bare name tells the computer which class to search.
- The form is
ClassName.methodName(), with a dot between the two parts. - The part before the dot names the class that defines the method.
Key Takeaways for Static Method Calls
- 1A static method's full name includes the class where it is defined, so the class name tells the computer which class to search.
- 2From another class, call a static method as
ClassName.methodName();, with the class name, a dot, and the method name. - 3A bare call such as
playGame(); works only inside the defining class and fails to compile from another class. - 4The class name lets
Poker.playGame(),Roulette.playGame(), andBlackjack.playGame()coexist without conflict.
At a glance
- A static method called from another class must be written as
ClassName.methodName(), with the class name, a dot, and the method name. - The class name tells the computer which class to search for the method.
- A bare call such as
playGame(); fails to compile when it is written outside the class that defines the method. - The class name lets several classes define a method with the same name without conflict.