• Register
1 vote
470 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.

2 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,240 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

Related questions

0 votes
1 answer 20 views
20 views
Problem: I'm new to Java and tried to get things running on my computer. A simple "hello world program" fails calling a method How to Remove the semicolon on the end of this line: public static void helloWorld();
asked Feb 14 Ali 5.7k points
0 votes
1 answer 24 views
24 views
Solve this problem.
asked Dec 28, 2020 TeamScript 13.5k points
0 votes
1 answer 4 views
4 views
Problem: I am getting a compile time error with Java: MyClass is not abstract and does not override abstract method onClassicControllerRemovedEvent( wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent) in wiiusejevents.utils.WiimoteListener)
asked Feb 17 Ali 5.7k points
0 votes
1 answer 24 views
24 views
Problem: In Java. Define the missing method. Use "this" to distinguish the local member from the parameter name.This error commonly occurs when i have a semicolon on your method declaration line. For example "public static void main(String args[]);". You should not have a semicolon there. Ensure that your methods have a body.
asked Feb 14 Ali 5.7k points
0 votes
1 answer 4 views
4 views
Problem: I'm tring to use update statement in ODI procedure. UPDATE tbl SET flag= CASE WHEN TO_TIMESTAMP(LOGDATE||' '||SUBSTR(LOGTIME,11),'DD-MON-YY HH:MI:SS AM') > CAST(#VAR_PREVIOUS_DATE AS TIMESTAMP) THEN 1 ELSE 0 END; im getting below error while executing it.
asked Feb 23 Ali 5.7k points
0 votes
1 answer 22 views
22 views
Problem: I'm trying to run the sample JavaFX project using IntelliJ but it fails with the exception : Error: JavaFX runtime components are missing, and are required to run this application I have the OpenJFX added with the 8 jars below "classes" and also the folders below " ... "Native Library Locations". When I'm building the project, it's good, but impossible to run it. What am I doing wrong?
asked Feb 14 Ali 5.7k points
0 votes
1 answer 5 views
5 views
Problem: In Java Programming, Can we call a static strategy for a theoretical class? Indeed I realize we can't utilize static with a technique for a theoretical class. in any case, I need to know why?
asked Feb 19 TeamScript 13.5k points
0 votes
1 answer 57 views
57 views
I want to know how to fix this. Could anyone here help me out?
asked Dec 16, 2020 TeamScript 13.5k points
0 votes
1 answer 23 views
23 views
Problem: I am new to the OOPS concepts. Is it compulsory for abstract classes to have abstract methods?
asked Sep 1, 2020 Raphael Pacheco 4.9k points
3 votes
2 answers 884 views
884 views
Problem: I am assuming a bunch of helpful souls are roaming here, in Kodlogs. I am trying to figuring out the abstract method in Java and wrote a simple program on it. Unfortunately, my program throws back an error and says, AbstractClassExample.java:9: error: AbstractClassExample ... (String args[]) { new AbstractClassExample().display(); } } Do you have any idea or solution for me? Thanks, guys.
asked Mar 25, 2020 Gavin 15.3k points