Why is the main function of Java a public static void main (string[] args)

Source: Internet
Author: User

This problem has plagued me for a long time, today on a check, after all, my curiosity is more heavy


1. Why ' public '

Because Java programs are called through the JVM virtual machine, the main () function must be public if it wants to be called


2.why "Static"

In Java, there is no static variable or function, if you want to be called, it is necessary to create a new object before you can. The main function, as the entrance to the program, needs to be started before other functions are instantiated, which is why it is added a static. The main function is like a door, to explore other functions to enter the program from the door first. Static provides an attribute that can be started without having to establish an object.


3.why "(string[] args)"?

(1) First of all, why this string array to call "args", in fact, this is a convention of a naming, not called "args" called Other is OK, do not believe that they can test themselves.

(2) To see why if a string array, Java is running like this (below), a bit like the command line, A1,A2,A3 is the command in this string array.

Java main A1 A2 A3


Let's go deep into the JVM driver and see how the main () function is started:

Mainid = (*env)->getstaticmethodid (env, MainClass, "main", "([ljava/lang/string;) V");

This is the JVM startup code, go directly to the static, parameter is a string array of the Main method.



/********************************

* This article from the blog "Bo Li Garvin"

* Reprint Please indicate the source : Http://blog.csdn.net/buptgshengod

******************************************/



Why is the main function of Java a public static void main (string[] args)

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: info-contact@alibabacloud.com 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.