Concepts / Static Methods and Their Definitions

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();

  • Programming

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?

  • It runs the playGame() method from Poker

  • It fails to compile

Reveal answer
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.

Poker.playGame()searches PokerplayGame();no class named to searchRoulette.playGame()searches RouletteBlackjack.playGame()searches Blackjack
When the call includes a class name, the compiler knows which class to search. When it does not, the name alone gives no class to search.

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.

java
Notes on the code
  • 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
Code walkthrough
public class Main {
    public static void main(String[] args) {
        Poker.playGame();
    }
}
  1. 1
    The public class Main contains the main method, which is where execution begins
  2. 2
    The statement Poker.playGame(); has the class name Poker, a dot, and the method name playGame
  3. 3
    The class name tells the computer to search Poker for playGame, so the method in Poker runs
  4. 4
    The 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
Output
Playing poker

Why 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.

Read playGame();Written inside thedefining class?Method found in thatclassNo class name tosearchFails to compileyesno, written in another class
The compiler reads the call and checks whether a class name was given. Without one, and with the call written in another class, the failure happens at the decision step.

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.

Pokerdefining classplayGame()public static methodMaincalling classPoker.playGame()qualified callcontainswritesselects
The Poker class contains playGame(). The call Poker.playGame() in Main selects that method through the class name.

Calling Two Classes That Share a Method Name

Worked example

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.

  1. 1
    Identify 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.

  2. 2
    Write the first qualified call

    Poker.playGame(); has the class name Poker, a dot, and the method name. It selects the method defined in Poker.

  3. 3
    Write the second qualified call

    Roulette.playGame(); uses the same method name but the qualifier Roulette, so it selects the method defined in Roulette.

  4. 4
    Check 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.

Result

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 like
    public class Main {
        public static void main(String[] args) {
            playGame();
        }
    }
    Why it fails

    The bare name playGame() does not tell the computer which class to search. Because Main is not Poker, the call fails to compile.

    Do this instead

    Write 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 fails

    The 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 instead

    Keep the order exactly as ClassName, a dot, then methodName().

  • Common mistake

    Assuming the bare call works because the method exists

    Why it fails

    The 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 instead

    Check whether the call is inside the defining class. If it is not, include the class name.

Practice: Fixing a Call from Another Class

EASY

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

  1. 1
    A static method's full name includes the class where it is defined, so the class name tells the computer which class to search.
  2. 2
    From another class, call a static method as ClassName.methodName();, with the class name, a dot, and the method name.
  3. 3
    A bare call such as playGame(); works only inside the defining class and fails to compile from another class.
  4. 4
    The class name lets Poker.playGame(), Roulette.playGame(), and Blackjack.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.