In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains why Java's main method must be public static void. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's learn why Java's main method must be public static void!
The Main method is the first method we learn about when we learn the Java programming language. Have you ever wondered why the main method is public, static, void? Of course, many people learn C and C++ first, but in Java the main method is slightly different from the former, it does not return any value, why the main method is public, static, void, this article tries to find some answers. The Main method is the entry point for Java programs, and remember that we're not going to talk about servlets, MIDlets, and any other container-managed Java programs here; in Java core programming, the JVM looks for public static void main(String[]args) in a class and throws an error NoSuchMethodError:main if it can't be found. The Main method must strictly follow its syntax rules, the method signature must be public static void, the parameters must be string array types, and if Java 1.5 and later versions can also use variable parameters:
public static void main(String... args)
Why is the main method static? 1. Because the main method is static, the JVM does not need to create any instances containing the main method to invoke it. 2. Because C and C++ also have similar main methods as entry points for program execution. 3. If the main method is not declared static, the JVM must create an instance of the main class because constructors can be overloaded and the JVM cannot determine which main method to call. 4. Static methods and static data loaded into memory can be called directly without creating an instance like instance methods. If the main method is static, it will be loaded into the JVM context as an executable method. Why is the main method public? Java specifies some accessible modifiers such as private, protected, public, any method or variable can be declared public, Java can be accessed from outside the class. Because the main method is public, the JVM can easily access and execute it. Why does the main method have no return value (Void)? Because main returns no value that makes sense to the program, it is designed to void, meaning that main does not return any value. The main method must be declared public, static, or void, otherwise the JVM cannot run the program. 2. If the JVM cannot find the main method it throws a NoSuchMethodError:main exception, for example if you run the command java HelloWrold, the JVM searches the HelloWorld.class file for public static void main (String[] args) methods. 3. The main mode is the entry point of the program, the beginning of program execution. 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" e.g. Exception in Thread main:Java.lang.NullPointedException, it means that the exception came from the main thread. 6. You can declare the main method using Java 1.5 variable arguments in ways such as publicstaticvoid main(String... args)。7. In addition to static, void, and public, you can use final, synchronized, and strictfp modifiers in the signature of main methods, such as publicstrictfpfinalsynchronizedstaticvoid main(String[] args). 8. The main method can be overloaded like any other method in Java, but the JVM will only call the main method of the signature specification above. 9. You can use the throws clause in method signatures to throw any checked and unchecked exceptions. 10. Static initialization blocks are executed before the JVM calls the main method, and they are executed as soon as the class is loaded into memory by the JVM. At this point, I believe that everyone has a deeper understanding of "why Java's main method must be public static void", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.