In the Java language, the main () method is the entry for the entire program, which is the main () method that is loaded first at run time, but this does not mean that the main () method is the first module to be executed when the program is run.
In the Java language, a static block of code is called when the class is loaded, so it can be executed before the main () method, using a static block of code to output Hello World before the main function
public class Staticc {static {System.out.println ("Hello World"); } public static void Main (string[] arg) {System.out.println ("Say hello to the world"); }}
Output:
Hello World
Say hello to the world
The execution order is independent of the location of the static code block
public class Staticc {/*static {System.out.println ("Hello World"); } */public static void Main (string[] arg) {System.out.println ("Say hello to the world"); } static {System.out.println ("Hello World"); }}
Output:
Hello World
Say hello to the world
"Java" outputs Hello World before the main function