Differences between static variables and non-static variables

Source: Internet
Author: User
    • Memory Allocation

Static variables in ApplicationProgramIt exists in the memory during initialization and disappears only when the program running of the class where it is located is completed;

Non-static variables need to be instantiated before memory allocation

Static variables are allocated when the class is loaded. The memory will be used for all objects created later. The corresponding operation is to operate the memory. It can also be seen as an alternative global variable.

    • Life Cycle

The life cycle of static variables is the application's life cycle;

The existence period of non-static variables depends on the existence period of the instantiated class.

    • Call Method

Static variables can only be called by "class. static variable name", but cannot be called by class instances;

Non-static variables can be directly accessed by the instantiated class name after the class where the variable is located is instantiated.

    • Share

Static variables are global variables shared by all class instance objects, that is, the value of static variables is changed for one instance, and those of other similar instances read the changed values;

Non-static variables are local variables and are not shared.

    • Access

Static members cannot access non-static members. Non-static members can access static members.

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.