Daniel Demesmaecker wrote:Without having declared main method static, your program will successfully compile but won't execute and report error at run time.
This is necessary since main is called by the Java interpreter before any objects are made.
The main is the method called when a Java application begins.
It is important to understand that the Java compiler will compile classes that do not contain a main method, but the Java interpreter has no way to run these classes.
Not any more. That trick ceased to work in Java7. You get an exception about no main method before the initialiser runs nowadays.
Dave Tolls wrote:. . . Java does load the class object for the class it is trying to run, running the above (java test.NoMain) will result in NoMain being loaded.
As part of being loaded the static code block is executed.
In the days when that trick did work, people followed the statements with System.exit(0); to prevent any error messages.Using JDK9u4Class members not marked public are accessible from other code in the same package; public access allows it to be run from outwith the package.
You do get an error, though, when it (finally) goes off to look for the main() method.
Dave Tolls wrote:Ah.
I did test it, but it's IntelliJ being too clever by half.
It's own launcher allows it to happen.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koophttps://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton