Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing method body, or declare abstract in Java

Tags:

java

I'm new to Java and tried to get things running on my computer.

A simple "hello world program" fails calling a method

class helloworld
{   
    public static void main(String[] param)
    {
        helloWorld();
        System.exit(0);
    }

    public static void helloWorld();
    {
        System.out.println("hello world");
    }
}

I get the following error:

.\helloworld.java:11: error: missing method body, or declare abstract
       public static  void helloworld();
                           ^
like image 909
cbatothinkofname Avatar asked Dec 13 '12 19:12

cbatothinkofname


3 Answers

Remove the semicolon on the end of this line: public static void helloWorld();

like image 85
Simon Forsberg Avatar answered Oct 22 '22 10:10

Simon Forsberg


This line:

public static void helloWorld();

is your problem. Ending a function with a semicolon implies you want it to be abstract and not have a body. This is similar to how methods in interfaces are declared, or if they are marked abstract i.e. no body.

like image 23
Woot4Moo Avatar answered Oct 22 '22 09:10

Woot4Moo


In this code line:

public static void helloWorld(); remove semicolon.

Make it like this:

public static void helloWorld()

To avoid this mistake further on, do it like this:

public static void helloWorld() {

An opening curly brace in the same line would make error detection easier.

like image 1
Shourya Pratap Singh Avatar answered Oct 22 '22 10:10

Shourya Pratap Singh