Static keyword Comprehensive understanding

Source: Internet
Author: User
The static or global meaning is used to modify classes, methods, and variables. Usually we hear class members or static variables (that is, they don't change after loading). When a static member is loaded, it will no longer change, such as string strings, once initialized the value will no longer change. Generally in development, we will share members with static modification.

1, the most common is: To modify the member variable (to become a member of the class), e.g.: Program status and exception information. (These two are generally decorated with static and final, which is common in development).

2, used to modify the member method, turn it into a class method, you can use the "type. Method Name" Call, commonly used in tool classes. (That is, you do not need to create objects)

3, static code block, on the initialization of multiple objects (static code block is executed earlier, only after the static variable)

static variables

Next we talk about the difference between static variables (modified by static) and instance variables (not modified by static variables):

1, static variables: In memory is just a copy, only initialized once, only once memory, with the type can be directly accessed, you can also create objects to access.

2, instance variables: Called to create an object, not created once to allocate memory once, in memory can have multiple tests, but do not affect each other.

Static methods

A static method can be called directly from a class name, or it can be called directly by an instance. So the super and the This keyword cannot exist in a static method (if there is not the same as bullying?). An instance variable is associated with a specific object).

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.