Do you really know about java static? code optimization may be simpler. javastatic

Source: Internet
Author: User

Do you really know about java static? code optimization may be simpler. javastatic

Static modified member variables and member methods are independent of any object in the class. That is to say, it is shared by all instances of the class and does not depend on a specific instance of the class.
As long as the class is loaded, the Java Virtual Machine can locate the class names in the Method Area of the runtime data zone. Therefore, a static object can be accessed before any of its objects are created without referencing any objects.

1. static variables

There are two types of static class member variables:

The difference between the two is:

Static member variables are initialized in the defined order. Static cannot modify local variables;

Therefore, static variables are generally used to implement the following functions:

  • When sharing values between objects
  • Convenient variable access
2. Static Method

Static MethodBenefitsIt means that you can directly call a class without generating an instance of the class. In this way, we can understand that the members modified using static are no longer objects, so they belong to the class and can be understood as common, that is to say, you can access it through the class name without consuming resources to create objects repeatedly, because it is already in the memory when the program is loaded for the first time and will not be released until the program ends. If it is not a static modified member, the memory will be recycled after use. Therefore, static should be used with caution, depending on the actual situation.

If this method is used as a tool, it is declared as staticYou don't need to use a new object. For example, to connect to the database, I declare a getConnection () method and define it as static, because the connection to the database is not unique to a specific object. It is used only as a tool to connect to the database. As for efficiency improvement, it is not necessary to determine whether the method is static based on its usefulness.

3. static code block

A static code block is also called a static code block. It is a static statement Block Independent of class members in a class. It can have multiple static code blocks and can be placed anywhere. It is not in any method body, when a JVM loads a class, it executes these static code blocks. If there are multiple static code blocks, the JVM executes them in sequence according to the sequence they appear in the class, and each code block is executed only once, so static blocks can be used to optimize program performance.

Differences between code blocks and Methods

Static code blocks are automatically executed;
Static methods are executed only when they are called.

Java Learning Group 669823128


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.