Why the Java Main method must be static
Look at the type of information today to find a problem, you can not print this keyword in the main method information, then remember the previous knowledge, can not call this in the static method. The reason is simple, this means "this object", which is the object that declares a class, whereas a static method does not belong to a particular object but belongs to the class. so the question is, why does the main function have to be set to static?
First, the main function acts as a portal for Java programs, can also be understood as an interface, this interface is not provided to the programmer to use, this is provided to users, such as the use of users, etc. do not need to know the implementation of the program inside, just need to know the interface on it, if it is not static, then when the user began to use , you must first create the object (call this method by the object name), and then where is the code to create the object? Oh, so this is difficult to clear.
Read a blog, write can also be reproduced over. The following content is from http://www.cnblogs.com/yangyisen/p/5108513.html.
The Main method is the first method we know when learning the Java programming language, and have you ever wondered why the main method is public, static, and void. Of course, many people first learn C and C + +, but in Java, the main method is slightly different from the former, it will not return any value, why main mode is public, static, void, this article tries to find some answers.
The main method is the portal to Java programs, remember that we don't discuss servlet, MIDlet, and any other container-managed Java programs, and in Java core programming, the JVM looks for public static void Main in the Class (String [] args, if the method is not found, an error nosuchmethoderror:main the program terminates.
The main method must strictly follow its syntax rules, the method signature must be public static void, the argument is a string array type, and the variable parameter can be used if it is Java1.5 and later versions:
Public static void Main (String ... args)
Why the Main method is static (static) because the main method is static, the JVM calls this method without creating an instance that contains the main method (that is, it no longer needs to create the object, it goes through the object instance, and the Java virtual machine finds the portal and starts executing directly). Because C and C + + also have similar main methods as portals for program execution. if the main method is not declared static, the JVM must create an instance of the main class, because the constructor can be overloaded and the JVM cannot determine which Main method is invoked. static methods and static data are loaded into memory and can be invoked directly without the need to create an instance like an instance method, and if the main method is static, it is loaded into the JVM context as an executable method. Why the main method is publicly owned (public)
java specifies some accessible modifiers such as private, protected, public, and any method or variable can be declared as Public,java can be accessed from a place outside of the class. Because the main method is public, the JVM can easily access to execute it.
Why the main method has no return value (void)
Because main returns no value to the program, the design is void, meaning that main does not have any values to return the summary Main method must be declared public, static, void, or the JVM will not be able to run the program If the JVM cannot find the main method, it throws a Nosuchmethoderror:main exception, for example: If you run the command: Java HELLOWROLD,JVM searches for the public static in the Helloworld.class file The main method of the void main (string[] args) is the entry of the program, at the beginning of the execution of the program. The Main method is run by a particular thread "main" (the thread name of the main method is main, regardless of the main method), and the program runs until the main thread ends or the Non-daemon thread terminates. When you see "Exception in thread main" such as: excpetion in thread main:Java.lang.NullPointedException, it means that the exception comes from the main thread You can declare that the main method uses java1.5 variable parameters in the form of:
Public static void Main (String ... args)
In addition to static, void, and public, you can use the final,synchronized, and STRICTFP modifiers in the signature of the Main method, such as:
Public STRICTFP final synchronized static void Main (string[] args)
The Main method can be overloaded (but not overridden) like any other method in Java, but the JVM will only invoke the main method of the signature specification above. You can use the throws clause in the method signature to throw any checked and unchecked exceptions
static initialization blocks are executed before the JVM calls the main method, which is executed when the class is loaded into memory by the JVM. Reprint Source: http://www.cnblogs.com/numen-fan/