Java: Call a method

Java: Call a method … here is a solution to the problem.

Java: Call a method

I’m learning how to create subroutines/methods in Java, but the problem I’m having is that I can’t call my method and the compiler thinks I’m calling (playGame();) is an attempt to define your own method. So I get the error “Invalid method declaration; Return type required”. Since I’m a beginner, I’m sure this is a stupid bug, but I’ve tried rewriting several times to fix it, but I can’t figure it out.

public class GUI {  
    public static void main(String[] args){

}
    public static void playGame() {

}
    playGame();
}

Solution

You can call only one method from another method, not from the body of the class. Move the line

playGame();

In the main method:

public static void main(String[] args){
    playgame();
}

Related Problems and Solutions