Home > Java > javaTutorial > body text

What is the use of main function in Java

WBOY
Release: 2023-05-04 09:34:06
forward
2053 people have browsed it

The general way of writing the main function is as follows:

public static void main(String[] args){…}
Copy after login

The functions of these keywords are explained below:

(1) public keyword, this is easy to understand, declaring the main function as public is to tell Other classes can access this function.

(2) The static keyword tells the compiler that the main function is a static function. That is to say, the code in the main function is stored in the static storage area, that is, this code already exists after the class is defined. If the main() method does not use the static modifier, there will be no compilation error, but if you try to execute the program, an error will be reported, indicating that the main() method does not exist. Because the class containing main() has not been 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.

(3) The void keyword indicates that the return value of main() is untyped.

(4) Parameter String[] args, this is the focus of this article.

First, program users can pass parameters to a class in the command line state.

Look at the following example:

public class ArgsDemo {  public static void main(String[] args) {  String str = new String();  for (int i = 0; i 《 args.length; i++) {  System.out.println(args[i]);  str += args[i];  }  System.out.println(str);  }  }
Copy after login

Use the javac ArgsDemo.java command to generate the ArgsDemo.class file; then use "java ArgsDemo parameter one parameter two parameter three..." The format of passing parameters to the ArgsDemo class. This example program will first output the parameters and then output the sum of all parameters. For example, java ArgsDemo a b c, you will get the following output:

a  b  c  abc
Copy after login

It should be noted that if the loop condition here is not i

Exception in thread "main" java.lang.ArrayIndexOutOfBoundException:3

at ArgsDemo.main(ArgsDemo.java:5)

Second, you can pass parameters to the class containing main() in another class.

The following example:

public class A {  public static void main(String[] args)  {  for(int i=0;i 《 p》  System.out.println(args[i]);  }  }  public class B {  public static void main(String[] args)  {  c = new A();  String[] b = {“111”,“222”,“333”};  c.main(b);  }  }
Copy after login

First define a class A and define it in A A main() function in which the parameter args are output. Then define a classB, initialize an instance c of A in B, then pass parameters to c, and call the main method of c to print out the passed parameter values. The output result is as follows:

111  222  333
Copy after login

Since the main() function is a static function, it can be used without instantiation, so B can also complete the same function using the following writing method:

public class B {  public static void main(String[] args)  {  //A c = new A();  String[] b = {“111”,“222”,“333”};  A.main(b);  }  }
Copy after login

The above is the detailed content of What is the use of main function in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!