Detailed introduction of main function in Java
Detailed introduction of main function in Java
The main function in Java is familiar to us. I believe everyone who has studied the Java language can skillfully write the entry function of this program, but not everyone can easily answer why the main function is written like this and what each keyword means. I also encountered this problem in my study. Through searching information on the Internet and my own practice, I finally had some experience. I didn't dare to keep it and wrote it out to share with you.
The main function is generally written as follows:
The functions of these keywords are explained below:
(1) public keyword, which is easy to understand. Declaring the main function as public means that other classes can access this function.
(2) static keyword, which tells the compiler that the main function is a static function. That is, the code in the main function is stored in the static storage area, that is, the code already exists after the class is defined. If the main () method does not use the static modifier, the compilation will not make an error, but if you try to execute the program, an error will be reported, Prompt that the main() method does not exist. Because the class containing main () is not instantiated (that is, there is no object of this class), its main () method will not exist. Using the static modifier means that the method is static and can be used without instantiation.
(4) parameter string [] args, which is the focus of this article.
First, the program user can pass parameters to a class in the command line state. Take the following example:
Use javac argsdemo Generate argsdemo. Java command Class file; Then use the format of "Java argsdemo parameter one parameter two parameter three..." to pass parameters to the argsdemo class. The sample program will output the parameters first, and then the sum of all parameters. For example, Java argsdemo a B C will get the following output:
It should be noted that if the loop condition here is not I
Second, you can pass parameters to the class containing main () in another class, as shown in the following example:
First, define a Class A, define a main () function in a, and output the parameter args in this function. Then define a ClassB, initialize an instance C of a in B, pass parameters to C, and call C's main method to print the passed parameter values. The output results are as follows:
Since the main() function is a static function and can be used without instantiation, B can also complete the same function by using the following writing method:
Summary: the main function of args parameter is to provide a means for program users to interact with the program in the command line state. In addition, it is also feasible to use the main () function directly in other classes and pass parameters. Although this method is not commonly used, it provides us with a choice after all.
If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!