• Register
1 vote
646 views

Problem:

Hello Mate,

I have a simple chunk of code written in Java programming language. I am trying to call a static function inside my main function. Everything is good except for one compile-time error. When I am trying to compile my program it throws back an error missing method body, or declare abstract

Find my code snippet below and fix the problem for me. I would appreciate your kind concern

public class SampleString{   
    public static void main(String[] args){
        sampleString();
        System.exit(0);
    }

    public static void sampleString();
    {
        System.out.println("I am a string!");
    }
}

Produced error is:

SampleString.java:7: error: missing method body, or declare abstract
    public static void sampleString();
                       ^
1 error

Where I've been doing wrong all the day? I am looking forward to hearing from you guys. Thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

4 Answers

1 vote

Solution:

Well, the problem occurred in the line 

public static void sampleString();

when you put the semicolon ( ; ) on the end of the line. Ending a function with a semicolon intends that you want it to be abstract and not have a body in this method. This is comparable to how methods in interfaces are declared.

To avoid such error the best practice is to put the curly brace end of the line like this

public static void sampleString(){
    System.out.println("I am a string!");
}

One more thing, you don’t need to put the line

System.exit(0);

within the main function. The main function will automatically be terminated by itself by executing the method sampleString();

I hope this might help you.

13 9 6
94,260 points
0 votes

Solution:

This error commonly occurs when you have a semicolon on your method declaration line. For example "public static void main(String args[]);". You should not have a semicolon there.

Example:

In this code line:
public static void helloWorld();

Because of this line, the code will show error. Remove semicolon

Make it like this:

public static void helloWorld()

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

public static void helloWorld() {

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

Understanding Common Errors In Java, are case sensitive. This means that if you declare a variable named average and try to later refer to it using Average, the compiler will complain that it cannot find a symbol named Average. Further, your main method header (the public static void main (String [] args) part is terminated by a ; which effectively closes the method with an empty method body. Remember: Method declarations (or anything that requires an {) must not have a semicolon ; following the statement. ; terminates statements.

10 6 4
31,120 points
0 votes

Solution:

first you focus on your error.. solution of error is (

abstract class HelloWorld
{
public abstract void(String[] args);

you shoud try this ;

7 4 2
28,600 points
0 votes

Solution:

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

7 4 2
28,600 points

Related questions

0 votes
1 answer 3 views
3 views
Problem: Please help me: java missing method body or declare abstract
asked Apr 7 rakib1 51.5k points
0 votes
1 answer 17 views
17 views
Problem: Help! Here is an error that I cannot really understand. What is exactly missing method body, or declare abstract?
asked Mar 26 ashik 14.6k points
0 votes
1 answer 5 views
5 views
Problem: Help me to fix this error must declare a body because it is not marked abstract ?
asked Apr 13 padma1 34.3k points
0 votes
1 answer 46 views
46 views
Solve this problem.
asked Dec 28, 2020 TeamScript 13.5k points
0 votes
0 answers 6 views
6 views
Problem: How does eclipse must declare a named package?
asked Apr 20 niyatiry 3.9k points
0 votes
0 answers 5 views
5 views
problem: please help me on this?the extract range has a missing or invalid field name
asked Mar 17 amnafahad 500 points
0 votes
1 answer 3 views
3 views
Problem: I'm not sure why i'm getting this error to be honest. private int hour { get; set { //make sure hour is positive if (value < MIN_HOUR) { hour = 0; MessageBox.Show("Hour value " + value.ToString() + " cannot be negative. Reset to " + MIN_HOUR.ToString( ... less than 24 hours //works even if the value is already within range, or value equal to 24 hour = value % MAX_HOUR; } } } Suggestions?
asked 4 days ago anika11 32k points
0 votes
1 answer 12 views
12 views
Problem: abstract methods do not specify a body.
asked Mar 13 Dan phillip 4.8k points
1 vote
1 answer 9 views
9 views
Problem: How to fix this error : c++ cannot declare variable to be of abstract type
asked Apr 7 MUHAMMAD MUNEEB 83k points
1 vote
1 answer 6 views
6 views
Problem: Can anyone tell me the solution of this problem? cannot declare variable to be of abstract type
asked Apr 7 MUHAMMAD MUNEEB 83k points