Why the main method of Java must be static

Source: Internet
Author: User
Tags instance method modifiers terminates

Tag: class read interface blank specific variable parameter href type End

Look at the type information today to find a problem, no longer the main method to print the information of this keyword, at this time remembered the previous knowledge, can no longer call this in the static method. The reason is simple, this means "this object", which is the object that declares a class, but static methods are not part of a particular object but belong to this class. So the question is, why does the main function have to be set to static?

First of all, the main function as the entry of Java programs, can also be understood as an interface, this interface is not provided to the programmer to use, when provided to the user, etc., users do not need to know the implementation of the internal program, just need to know the interface can be, if non-static, then when the user started to use, You have to create the object first, then where is the code that created the object written? Oh, so this is very difficult to clarify.

Read a blog, write can also, 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 we learn the Java programming language, have you ever wondered why the main method is public, static, 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 the main way is public, static, void, this article tries to find some answers.

The main method is the portal to the Java program, and remember that we will not discuss the servlet, MIDlet, and any other container-managed Java programs, in Java core programming, the JVM looks for public static void Main (string[) in the class args), throws an error if the method is not found 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 if it is Java1.5 and later versions, you can also use mutable parameters:

 Public  Static void Main (String ... args)

Why is the Main method static? (static)

    1. Because the main method is static, the JVM calls this method without having to create any instances that contain the main method.
    2. Because C and C + + also have a similar main method as the entry for program execution.
    3. 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 to invoke.
    4. static methods and static data are loaded into memory and can be called directly without having to create an instance like an instance method to invoke, and if the main method is static, it will be loaded into the JVM context as an executable method.
why the Main method is 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 somewhere outside of the class. Because the main method is public, the JVM can easily access the execution of it.

Why does the main method have no return value (Void)

Because main returns any value to the program, it makes no sense, so the design is void, which means that main does not have any value to return

Summarize
    1. The main method must be declared public, static, void, or the JVM cannot run the program
    2. If the JVM does not find the main method, it throws a Nosuchmethoderror:main exception, for example: If you run the command: java HelloWrold the JVM will search the Helloworld.class file for public static void main ( String[] args) Release method
    3. The main way is the entry of the program, the beginning of the execution of the program.
    4. The Main method is run by a specific thread, "main", and the program runs until the main thread ends or the Non-daemon thread terminates.
    5. 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
    6. You can declare how the Main method uses java1.5 's mutable arguments, such as:
       Public  Static void Main (String ... args)
    7. 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)
    8. The main method can be overloaded in Java like any other method, but the JVM will only invoke the main method of the signature specification above.
    9. You can use the throws clause to throw any checked and unchecked exceptions in the method signature
    10. Static initialization blocks are executed before the JVM invokes the main method, and they are executed when the class is loaded into memory by the JVM.

Why the main method of Java must be static

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.