Java Basic notes--static modifier

Source: Internet
Author: User

1.static

Any variable or code in the program is automatically allocated by the system at compile time to store the memory, and so-called static refers to the memory allocated after compilation will persist until the program exits the memory to release the space, that is, as long as the program is running, then this memory will persist. What is the point of doing this?

In a Java program, everything is an object, and an object's abstraction is a class, and for a class, if you want to use his members, you would normally have to instantiate the object before you can access those members by reference to the object, but there is an exception. Is that the member is declared with static (the access control of the class is excluded here).

Static is a class method that is loaded only once, but always consumes memory. More resource consumption, and the instance method (non-static) needs to be loaded more than once, but does not always consume memory. Less resource consumption. In general, in the program, some of the use of high-frequency methods with static methods, low-frequency use of the example method.

If all the methods become static methods, then the Java language will degenerate into a process language, so it is absolutely not possible.

A typical static method:

1  Public Static void Main (String ... args) {  2     //  ...   3 }  

In addition to the above main, there are, for example, some operations on the class itself, such as: Using the class's count, or the tool class method (Math.sin (), Thread.Sleep ()), and so on.

 Packagetest; Public classStatictest {Private StaticString S1; PrivateString S2;  Public Static voidMain (string[] args) {S1= "Hello, static";         System.out.println (S1); Statictest D=Newstatictest (); D.s2= "Hello, non-static!"; SYSTEM.OUT.PRINTLN (S2);//here will be an error, prompting to convert the S2 to staticSystem.out.println (D.S2);//This can be printed, and the instance method must be referenced by the object}}

Java Basic notes--static modifier

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.