In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what is the reason why the main method in Java must be public static void". In daily operation, I believe that many people have doubts about the reason why the main method in Java must be public static void. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt that "the main method in Java must be public static void". Next, please follow the editor to study!
In Java, no one is new to the main method, which is the entry method for Java applications. When the program is running, the first method to execute is the main method.
When we write down the first hello world using Java, we need to create a main method, and when we use Spring Boot to start a web application, we also need a main method.
When we want to create a main method in Intellij IDEA, just type psvm to automatically help create a main method:
After we get a main method, I don't know if you have noticed that at any time, the main method we are going to create is in the same form:
Public static void main (String [] args) {
}
First of all, they are all public and static, the return values are all void, the method names are main, and the input parameters are all an array of strings.
The only part that can be changed in the above method declaration is the parameter name of the method. You can change the args to whatever name you want to use.
Of course, the main method can also be written in the following form, but it makes no difference:
Public static void main (String... Args) {
}
So, have you ever wondered why the main method has to be of type public static void and its parameters have to be an array of strings?
How to start the Java virtual machine
In the Java language specification, the startup of the Java virtual machine is clearly defined: the Java virtual machine is started by loading the specified class and then calling the main method in that class.
That is, you can start the Java virtual machine by calling the main method of a specified class and passing him a single string array parameter.
For a main method to be executed, it takes several steps. First, the corresponding class needs to be loaded by the virtual machine, then it needs to be linked and initialized, and then the main method is called.
Then a method wants to be called, depending on its access qualifier and method type, the conditions to be called are also different.
Why is the main method public?
In Java, access controllers can be used to protect access to classes, variables, methods, and constructors. Java supports four different access rights.
Default (that is, nothing is written by default): visible in the same package without any modifiers. Use objects: classes, interfaces, variables, methods.
Private: visible within the same category. Use objects: variables, methods. Note: classes cannot be modified (external classes)
Public: visible to all classes. Using objects: classes, interfaces, variables, methods
Protected: visible to classes and all subclasses in the same package. Use objects: variables, methods. Note: classes (external classes) cannot be modified.
All four controls can be used to modify a method, but the access permissions of the decorated method are different.
For the main method, we need to call him directly through JVM, so his qualifier must be public, otherwise it is inaccessible.
Why is the main method static?
Static is a static modifier, the method modified by him is called static method, static method has a characteristic, that is, static method is independent of any object of this class, it does not rely on class-specific instances, and is shared by all instances of the class. As long as this class is loaded, the Java virtual machine can find them based on the class name in the method area of the run-time data area.
For the main method, its calling process goes through class loading, linking, and initialization. But it has not been instantiated, so if you want to call a method in a class. Then this method must be static, otherwise it cannot be called.
Why does the main method not return a void?
If you have some knowledge of C language and C++ language, you will know that programming languages like C and C++ use int as the return value of the main function.
This return value, which is the exit code when the program exits, is usually called by the command interpreter or other external program to determine whether the process is complete. Under normal circumstances, a book is returned with 0, and non-0 means abnormal exit.
In Java, the exit process is controlled by JVM, and the program terminates all its behavior and exits when the following two situations occur:
1. All threads that are not background daemon threads are terminated.
2. A thread calls the exit method of the Runtime class or the System class, and the security manager does not prohibit the exit operation.
In the above two cases, once the second situation occurs, JVM does not care whether the main method has been executed or not, he will terminate all behaviors and exit, then the return value of the main method is meaningless.
Therefore, the return value of the main method is invariably required to be void.
Why is the input parameter to the main method an array of strings (String [])
Java applications can accept parameters from the command line, and parameters passed from the command line can be received in java programs and can be used as input.
Because command-line arguments are ultimately passed as strings, and sometimes there is more than one command-line argument, it is possible to pass multiple arguments.
At this point, as the entry point for the execution of the Java application, the main method needs to be able to accept these multiple string parameters, so use the string array.
At this point, the study of "what is the reason why the main method in Java must be public static void" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.