Hello world works but then gets error that there's no main?

前端 未结 6 589
星月不相逢
星月不相逢 2020-12-09 09:57

I have the following simple hello world in Java:

class A {
    static {
        System.out.println(\"Hello world\");
    }
}

It works as ex

相关标签:
6条回答
  • 2020-12-09 10:34

    When your class is loaded, static initializers will be run, and then the JVM will try to invoke the main method. You can fix this by adding one more line to your static initializer:

    public class HelloWorld {
        static {
            System.out.println("Look ma', no hands!");
            System.exit(0);
        }
    }
    

    This will stop the JVM before it tries to invoke your main method.

    Also note, this will not execute in Java 7. Java 7 looks for a main method before initializing the class.

    0 讨论(0)
  • 2020-12-09 10:39

    You do need a main method. In your last code example you've defined it wrong. Define it like this:

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

    You were missing the args that's why it still complained the second time.

    @WilliamMorrison and @Jeffrey have explained why your program outputs "Hello world" and then it crashes.

    There is a very good reason to use main instead of the way you're using the static block. Mainly, that it's the entry point into your program. You can't successfully run your program from the java command without one. You can't execute a jar file without one. And, as you've seen, you'll If you're missing a main, you'll get this error every time unless you write a hack to prevent it (see @Jeffrey's answer. I don't mean that in an offensive way, his answer is very informative, but it is a hack IMO).

    Another thing is that this is not idiomatic. If I had to read your code I'd be wondering, "Where does this freaking code start?!" I wouldn't be able to download it and figure out how to get my tools (IDE, maven, ant, etc) to run it. Intellij doesn't know it can run a class without a main method. That is the standard entry point into a program.

    A random static block is not an entry point. There's no way to pass arguments into it. For example, if you wanted to write a command line program like unix's "cat" where you pass in a file name and it outputs the file contents, you couldn't do that with a static block because there would be no way to set and get the input.

    0 讨论(0)
  • 2020-12-09 10:41

    Its because your class is loaded by the class loader, static code is executed, then it searches for the main method and crashes as it doesn't exist.

    Code in a class wrapped in a static block which is outside any method will execute upon first reference of that static class.

    The reason it can't find your main method is you've declared it incorrectly. Main looks like this:

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

    Get used to that line. You'll see it many, many times.

    The String[] args... argument is a list of space delineated arguments a user can pass to your program through a shell. All programs enter through the main method. This is a standard passed down for some time.

    A static block could be used in a class to start your program, but it would cause confusion and be extremely improper. Using a main method as an entry point is the industry standard.

    I am unable to give a reason why this is the standard, or give a more convincing argument as to why to use it other than to get input into your program from a shell. I do not suggest starting your program from a static block.

    0 讨论(0)
  • 2020-12-09 10:42

    Forget the static block, and do it like this:

    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
    
    0 讨论(0)
  • 2020-12-09 10:43

    It works as expected, but oddly: No. class gets loaded. And when a class is loaded, its static blocks are executed.

    Why you get Exception in thread "main" java.lang.NoSuchMethodError: main

    Because in first case you dont have a main method and in second case signature is not correct.

    change it to public static void main(String a[])

    0 讨论(0)
  • 2020-12-09 10:43

    When classes are loaded to the JVM, it first "process" what is in the static block, which is why it prints out "Hello World" first, which explains this part:

    class A {
        static {
            System.out.println("Hello world");
        }
    }
    

    When you run the program it looks for the "main" method, hence it prints out the Hello World first then looks for the main method, hence the error, same logic is applied when you call the method inside the static block.

    0 讨论(0)
提交回复
热议问题