Java's static

Source: Internet
Author: User

In Java, you can create multiple objects based on a class that are independent of each other and have their own members. But sometimes we need to share the same member, it can be very convenient to operate, so there is our static.  

Static can modify variables, methods, and code blocks

First, static modified variables

A static modified variable is called a statically variable or a class variable, which belongs to a category and is not unique to an object and can be modified by all objects of the class. For example, a persimmon in the wild in the persimmon tree, usually through the per capita can be picked, and after the people see the results are the first to go to the people after picking the results, this is the static persimmon.

Static variables can be accessed using classes, or they can be accessed using objects, so we recommend the use of class access due to its particularity.

Here is an example of a static variable

the results of the operation are as follows:

That is, when an object modifies the value of a static variable, the result of the other object invocation is modified, and the shared variable is implemented.

Second, the method of static modification

A. Static variables can be called in a static method, but non-static variables cannot be called

If you want to invoke a non-static variable, you can create an object of the class to access

The main method is a static method

B. Static methods can be called static methods, but non-static methods can not be called, and the above variables are the same, create object access non-static method

C. Non-static variables, non-static methods, static variables, and static methods can be accessed in common methods

Summary sentence: All static is everyone's (can go to visit), what is non-static is private (except for others, who do not want to visit)

Third, static modifier code block

Code blocks in Java can initialize and assign values to variables

These blocks of code can be executed sequentially when the instance is created, which is called static initialization blocks

Static initialization blocks are executed once when the class is loaded, no longer executed at a later time, and only the static variables are initialized, and non-static variables cannot be initialized.

The above code shows the relationship between the construction method, the normal code block, and the static initialization block: a). The first time you create an object, you execute the code block in turn, but the order of execution is static initialization of the block----> Normal code block------> Construct method B) static code block executes only once

These are some of the use of static, summed up here .

Java's static

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.