Differences between static variables and non-static variables

Source: Internet
Author: User

Differences between static variables and non-static variables

Before static variablesStaticKeyword modification.

1. Memory Allocation

Static variables exist in the memory during application initialization until the end of the program running of the class in which the static variables are stored;

Non-static variables need to be instantiated before memory allocation.

2. Lifecycle

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.

3. Call Method

Static variables can only be passed through.Static variable name "call, class instance cannot call;

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

4. Sharing Mode

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. That is, the non-static variables accessed by different instances of a class can be different values.

5. Access Method

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.