Are you really aware of Java static, code optimization may be easier

Source: Internet
Author: User

The static decorated member variables and member methods are independent of any object of the class. That is, it does not depend on class-specific instances and is shared by all instances of the class.
As long as this class is loaded, the Java virtual machine can find them based on the class name in the method area of the run-time data area. 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 class member variables that can be classified according to whether they are static:

    1. A variable that is modified by static, called a static variable or a class variable;
    2. The other is a variable that is not modified by static, called an instance variable.

The difference between the two is:

    1. For static variables there is only one copy in memory (memory saving), the JVM allocates only one memory at a time, completes the memory allocation of static variables during the loading of the class, can be accessed directly (conveniently) by the class name, and, of course, is accessible through objects (but this is not recommended).

    2. For instance variables, when an instance is not created, the instance variable is allocated one memory, and the instance variable can have multiple copies in memory without compromising (flexibility).

The initialization order of static member variables is initialized in the order in which they are defined. Static can not modify local variables;

As a result, static variables are typically used when you need to implement the following two functions:

    • When a value is shared between objects
    • Easy access to variables
2. Static method

The advantage of a static method is that it can be called directly without generating an instance of the class, so that members that use the static modifier are no longer returned to the object so, instead of being a class that can be understood to be common, it is said that as long as the class name is accessible, there is no need to consume resources to create objects repeatedly. Because it is already in memory when the program first loads, it will not be released until the program finishes the memory. If a member is not static decorated, the memory will be recycled after use, so static should be used with caution, depending on the actual situation

If this method is used as a tool, it is declared as static, it can be used without the new object, such as connecting to a database, I declare a getconnection () method, it is defined as static, Because connecting to a database is not specific to an object. It is used only as a tool to connect to a database. As for efficiency, it is not necessary to look at the usefulness of specific methods to define whether the method is static.

3. Static code block

A static block of code is also called a code block, which is an independent block of static blocks in a class that is separate from a class member, can have more than one position, it is not in any method body, and the JVM executes these static blocks of code when the class is loaded, and if there are multiple static blocks of code, The JVM executes them sequentially in the order in which they appear in the class, and each block is executed only once, so that static blocks can be used to optimize program performance.

The difference between code blocks and methods

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

Java Learning Group 669823128


You really know about Java static, code optimization might be easier

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.