JAVA: Start from public static void main (String args[])

Source: Internet
Author: User

We all know that when you want to execute a Java file, you need to have a main function.

What is this for?

As with the C language, when you execute a file. You will need to have an entry function or an entry address, which is the main function in C. The same entry function in Java is the public static void main (String args[]). This is the Java program's entry address, the Java Virtual machine executes the program is the first to find the main method. The function of the main () function is the same as the C language. Only Java programs with the main () method can be used by Java virtual machines, which can be understood as a prescribed format.

(1) Publickeyword, this good understanding. Declaring the main function public is telling other classes to access the function.
(2) Statickeyword, tell the compiler that the main function is a static function.

That is, the code in the main function is stored in a static storage area, that is, when the class is defined, the code already exists.

Assuming that the main () method does not use the static modifier, the compilation does not go wrong. But suppose you try to run the program and you will get an error. Tip the main () method does not exist. Because a class that includes main () is not instantiated (that is, an object without this class), its main () method does not exist.

Using the static modifier means that the method is static and can be used without instantiation.
Because Java is a completely object-oriented language, everything is based on objects, and the advantage of static is that the main function does not rely on the object of the class. He belongs to the class. However, when the class is loaded, he is also loaded into the virtual machine, no need to instantiate.

(3) Voidkeyword indicates that the return value of main () is untyped.
(4) References string[] args.

There are two types of Mian function receive parameters

First, the user of the program can pass a reference to a class in the command line state. Look at the following examples:

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); }  }

Use the Javac command to generate the Argsdemo.class file, and then pass the parameters to the Argsdemo class using the format "java Argsdemo parameter two of three ...".

The Demo sample program outputs the parameters first, and then outputs the total number of parameters. Java Argsdemo a B c, for example, will get this output:
It is important to note that. Let's say the loop condition here is not I <args.length. But I <5. The input on the command line must be 5, otherwise the error will be:

Exception in thread "main" Java.lang.arrayindexoutofboundexception:3 at Argsdemo.main (

Second, you can pass a parameter to a class that includes main () in a class, such as the following example:
public class A {public static void main (string[] args) {for (int i=0;i <args.length;i++) 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); }  }

First define a Class A, define a main () function in a, and output the parameter args in the function.

Then define a CLASSB, initialize the instance C of a A in B, and pass the parameters to C. and call C's Main method to print out the incoming number of parameters. The output results are as follows:
Because the main () function is a static function. That is, it does not need to be instantiated. So B can also complete the same function using the following notation:

public class B {public static void main (string[] args) {//a c = new A ();  String[] B = {"111", "222", "333"};  A.main (b); }  }

Reference article Java_main_ function

JAVA: Start from public static void main (String args[])

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.