When I first came into contact with the Java program, the teacher taught us to start learning from the following sentence. It is said that it is customary. So until today, I still only know that the java program should be written like this, I am ashamed of the specific reason.
System.out.println("Hello Java" }
Since I am so ashamed of myself today, it is time to understand why the main functions of java should be written in this format. Let's explain from the following points.
Why use public static to modify main ()
In Java, the main () method is the entry method of java applications. The Java virtual machine uses the main method to locate the running program to be started, and checks whether the class of the main function is loaded by the Java Virtual Machine. If it is not loaded, the class is loaded and all other related classes are loaded. Therefore, when the program is running, the first method to execute (note that it is a method, rather than other methods, such as code blocks) is the main () method. Generally, if you want to run a class method, you must first instantiate an object of the class and then use the "Object Name. method Name () "to run the method, but because main is the entry of the program, the object has not yet been instantiated, so the main method is declared as static, in this way, you can directly use the class name. method.
Why use void to modify main ()
Description of String [] args
The main () method can throws Exception.
When I saw that the main () method could not throw an exception on the Internet and felt a bit confused, I tried it myself. The Code is as follows, and it runs normally.
main(String[] args) System.out.println("Hello World" Exception("hello world" }
Summary
By describing the modifier of main (), we finally understand all of the parameters and do not understand them at all. But there is still some confusion. Stay here and hope you can advise me. Q: Why is the static code block in the Code executed before the main () function?
"Hello Wordld!"