Java why the main function in front to add static modifier __ function

Source: Internet
Author: User
Tags modifier

Let's talk about static.

1.static decorated domain, we call the static domain, which is classified all, is shared by all objects, and only one
2.static decorated chunks, the field is only initialized once
3.static a field or method that can be modified directly by the name of the class. To invoke
Non-static members cannot be used directly within a 4.static decorated method

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 store, where the static method is fixed in memory, meaning that the code already exists after the class has been defined. If the main () method does not use the static modifier, the compilation will not go wrong, but if you attempt to execute the program it will be an error, prompting the main () method does not exist. Because a class that contains main () is not instantiated (that is, an object that does not have this class), its main () method does not exist. Using the static modifier, however, means that the method is static and does not need to be instantiated to use.

The main method is the entrance to a program, if written as non-static, then you must instantiate an object and then call it, since it is the portal is certainly not possible; static methods are classes, and can be invoked directly with class names.

Static methods do not need to create objects to directly access such a main method without having to do special processing

The static method is in the quiescent data area, so that you can invoke it directly without requiring each call to be regenerated.

Static functions are not instantiated when invoked

Static, loaded into memory on the first
The main function is actually a method of the class, as if a class is test, then the main of the class is actually Test.main (string[] args), and it is well known that if a method is not static, then instantiate the class first, such as Test t=new Test (); Then the Test.main () can be invoked;
This is not realistic for running the main function of a program, so for example, the main function is defined as static so that Test.main () can be invoked directly.
Because main is the main function of this class, the portal, if you go to the portal each time and then go to the instance and then call the entry method, it's verbose.

Note: The above a lot of things on the internet, the most wonderful answer is in csdn, a problem caused by the deep answer, and finally Aristotle came out, interested onlookers bar http://bbs.csdn.net/topics/200084223.

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.