According to the explanation, it'll throw a NoSuchMethodError since this main method returns long rather than void. The error, I'm getting, however, is as follows:
This does happen at run time though. The code compiles without problems.
Is this error a NoSuchMethodError? If it is, why would the error name be omitted?
In the explanation of another Enthuware question, it says:
You can have a main method that doesn't take String as an argument. It will not make the class executable from the command line though.
It seems to me that this is what's happening here. I can't execute it from the command line. Makes sense. But that doesn't explain why the first example, according to the explanation, throws a NoSuchMethodError (which it doesn't when I compile and run).
Just for completeness: the changed behavior only applies to the main(String) method. So if you try to invoke a non-existing method, you still can get a NoSuchMethodError at runtime.
If you wonder how you can invoke a non-existing method without getting a compiler error, here is an example. Assume the following classesBoth classes can be successfully compiled and if you run TestGreetings, the output will be (obviously) hi.
Now make a change to the signature of the hi() method (e.g. rename to sayHi) and only compile the Greetings class. If you now run TestGreetings, you'll get the following output:
Exception in thread "main" java.lang.NoSuchMethodError: Greetings.hi()V
Please note that although the return type is not used in the TestGreetings class, a change of the return type of the hi() method (and of course adding the appropriate return statement) will also result in a NoSuchMethodError when you run TestGreetings.