Main function details,

Source: Internet
Author: User

Main function details,

1. The main function is static;

2. The main function is a special function, which can be recognized by JVM as the program entry;

3. Main Function Definition:

(1) public:

① Public, which indicates that the permission is the largest and can be accessed under any circumstances;

② Reasons for using public: to ensure that the JVM can access the main method under any circumstances;

(2) static:

① Static and static can make it easier for the JVM to call the main function without calling the object;

② If you do not use static modification, you need to create an object call (this JVM can solve it by yourself), but the JVM does not know how to create an object, some parameters are required for object creation. JVM cannot solve the problem of passing parameters.

class Demo1{public void main(String[] args){System.out.println("Hello world");}}

For example, if the above Code does not use static modification, the JVM must create an object when calling the main function. Therefore, the main method is called through new Demo1.main (), so there is no problem, the most feared case is that this class has no construction method without parameters, and there is only one construction method with parameters.

class Demo1{public Demo1(String name,int age){}public void main(String[] args){System.out.println("Hello world");}}

In this case, the main method can be called only after an object is created without static Modification on the JVM. when an object is created, the jvm cannot solve the problem, that is, passing parameters. Because the object to be created requires a constructor, but there is no constructor without parameters, you can only use this constructor with parameters, but this constructor needs to receive parameters. For JVM, it does not know what parameters should be passed. Therefore, it is very troublesome for JVM to create objects. Therefore, static is used at this time to avoid these troubles.
(3) void:

No value is returned because the returned data is sent to JVM, And it is meaningless for JVM to use this data. Therefore, the returned data is null.

If the return type is not void but int, then int is returned to JVM, and JVM cannot give this data to you, because after many lines of code are written in your main method, the last return statement is returned. Once returned, it means that this method is over. After the main method is over, it means that your program has stopped. At this time, even if it is returned to JVM, you cannot get this data, this data is meaningless for JVM.

(4) main:

Main is the function name;

Note: main is not a keyword, but a special function name that JVM can recognize. It tells JVM that this is the entrance to the program.

(5) (String [] args ):

Function parameters, worried that some programs need parameters when starting. The parameter type is an array, and the elements in this array are strings and strings.

4. Instance

Class Demo1 {public static void main (String [] args) {System. out. println ("length of the array:" + args. length); for (int I = 0; I <args. length; I ++) {System. out. print (args [I] + ",");}}}

Shows the running result:

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. Http://

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.