Search code examples
javamethodssubroutine

java: calling a method


I am just learning how to create a subroutine/method in java, and I am having the problem that I can't call my method with the compiler thinking that my call (playGame();) is an attempted definition of a method of itself. So i get the error "invalid method declaration; return type required". As I am a beginner I am sure that it is a stupid mistake, but I have tried rewriting many times to fix it and I cannot figure it out.

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

    }
    public static void playGame() {

    }
    playGame();
}

Solution

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

    playGame();
    

    inside the main method:

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