[Javase Study Notes]-7.9 differences between member variables and static variables

Source: Internet
Author: User

[Javase Study Notes]-7.9 differences between member variables and static variables

In this section, let's take a look at the differences between member variables and static variables.

What are static variables? We mentioned static variables when we used the static keyword in the previous section. That is to say, the variable modified with the static keyword is a static variable.

We learned the differences between member variables and local variables in Section 6.4. This section focuses on the differences between member variables and static variables.

Let's look at the Code:

Class Person {String name; // name, which is a member variable static String country = "China"; // nationality, which is a static variable public void printInfo () {System. out. println (name + ":" + country );}}

In the code above, we can see that the variable name is a member variable, while the country is a static variable, which is modified with the static keyword.

Here we summarize the differences between the characteristics of the static keyword and the features of member variables in the previous section:

1. different lifecycles of two variables:

Member variable: It exists with the creation of the object and is released with the collection of the object.

Static variable: It exists with the loading of the class and disappears with the disappearance of the class.

When will the class disappear? Generally, when the Virtual Machine ends, the class ends. Of course, this is just a general case.

2. The call methods of the two variables are different:

Let's take a look at the test of the above Code:

Class StaticTest {public static void main (String [] args) {Person p = new Person (); p. name = "Xiaoqiang"; String name = Person. name; System. out. println (p. country + "--" + Person. country );}}
Result:


We can see that calling the member variable directly using the class name will cause the error "unable to access non-static variables from static context.

Run the following command after commenting this statement:

Class StaticTest {public static void main (String [] args) {Person p = new Person (); p. name = "Xiaoqiang"; // String name = Person. name; System. out. println (p. country + "--" + Person. country );}}
Result:

The program runs normally and has legal access, so we can summarize the following differences:

Member variables: can only be called by objects.

Static variables: they can be called by objects or classes. In the previous section, we learned that static modified members can be called directly using class names.

3. The aliases of the two variables are different:

Member variable: Also known as instance variable.

Static variables: Also known as class variables.

4. The storage locations of the two variables are different:

Member variable: it is stored in the object in the heap memory, so it is also called the object's special data.

Static variables: these variables are stored in the static zone of the Method Area (shared data area). Therefore, they are also called shared data of classes.

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.