编程知识 cdmana.com

Today, I finally understand why the main method of Java must be public static void?

Main The way is that we learn Java The first way you know about programming languages , Have you ever thought about why main The method is public、static、void Of . Of course , The first thing many people learn is C and C++, But in Java in main The method is slightly different from the former , It doesn't return any value , Why? main The way is public、static、void, This article tries to find some answers .

Main The method is Java Program entry , remember , We will not discuss Servlet、MIDlet And any other container java Program , stay java In core programming ,JVM Will look for public static void main(String[]args), If the method is not found, an error is thrown NoSuchMethodError:main Termination of procedure .

Main Method must strictly follow its grammar rules , Method signature must be public static void, Parameter is string array type , If it is Java1.5 And later versions can also use variable parameters :

public static void main(String... args)

Why? main The method is static (static)?

1. It is because main The method is static ,JVM Calling this method doesn't need to create anything that contains this main Examples of methods .

2. because C and C++ There are also similar main Method as the entry point for program execution .

3. If main Method is not declared static ,JVM You have to create main Class , Because constructors can be overloaded ,JVM There's no way to determine which main Method .

4. Static methods and static data can be called directly when they are loaded into memory without creating an instance like an instance method , If main The method is static , Then it will be loaded into JVM Make executable methods in context .

Why? main The method is public (public) ?

Java Some accessible modifiers are specified such as :private、protected、public, Any method or variable can be declared as public,Java You can access... From outside of this class . because main The method is public ,JVM You can easily access and execute it .

Why? main Method does not return a value (Void)?

because main Returning any value means nothing to the program , So it's designed to be void, signify main No value will return .

summary

1.main Method must be declared as public、static、void, otherwise JVM Can't run the program .

2. If JVM Can't find main Method throw NoSuchMethodError:main abnormal , for example : If you run the command :java HelloWrold,JVM Will be in HelloWorld.class Search for public static void main (String[] args) Method .

3.main The way is the entry to the program , The beginning of program execution .

4.main Method is used by a specific thread ”main” function , The program will run until main End of thread or non-daemon Thread termination .

5. When you see “Exception in Thread main” Such as :Excpetion in Thread main:Java.lang.NullPointedException, It means that the exception comes from main Threads .

6. You can declare main Methods use java1.5 The way of variable parameters is as follows :publicstaticvoid main(String... args).

7. except static、void、 and public, You can use final,synchronized、 and strictfp Modifiers in main Method's signature , Such as :publicstrictfpfinalsynchronizedstaticvoid main(String[] args).

8.main Method in Java It can be overloaded like other methods , however JVM Only the above signature specification will be called main Method .

9. You can use throws Clause in method signature , You can throw anything checked and unchecked abnormal .

10. The static initialization block is in JVM call main Method is executed before , They are in class JVM It's executed when it's loaded into memory .

source :suo.im/6v9d64

版权声明
本文为[Manong's breakthrough]所创,转载请带上原文链接,感谢

Scroll to Top