[Javase Learning Note the difference between a member variable and a static variable]-8.2

Source: Internet
Author: User

In this section we look at the difference between a member variable and a static variable.


What is a static variable? We talked about static variables when we were in the Statickeyword. In other words, a variable modified with Statickeyword is a static variable.

We learned the difference between a member variable and a local variable in section 6.4. In this section we'll focus on the difference between the member variable and the static variable.

Let's look at the code:

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

In the above code we can see that the variable name is a member variable, and country is a static variable, decorated with Statickeyword.

Here we summarize the differences in four areas through the characteristics of the previous section Statickeyword and the characteristics of the member variables:


1. Two variables have a different life cycle:

Member variables: As objects are created, they are released as objects are recycled.

Static variable: exists as the class is loaded. Disappears as the class disappears.

So when does the class disappear? Under normal circumstances, the virtual machine ends, and the class ends. Of course, this is just the ordinary case.


2. Two variables are called differently:

Let's look at a test of the above code:

Class Statictest {public static void main (string[] args) {person p = new person ();p. Name = "Xiao Qiang"; String name = Person.name; System.out.println (p.country+ "--" +person.country);}}
Results:


We were able to see the error "Cannot access non-static variables from the static context" by invoking the member variable directly with the class name.

When we gaze upon this remark, we will do it again:

Class Statictest {public static void main (string[] args) {person p = new person ();p. Name = "Xiao Qiang";//string name = Person.nam E System.out.println (p.country+ "--" +person.country);}}
Results:


The program executes properly and is legally interviewed, so we can summarize the following differences:

Member variables: can only be called by an object.

Static variables: Can be called by an object. can also be called by the class.

In the previous section, we have learned that members of the static modifier can be called directly with the class name.


3. Two variables have different aliases:

Member variables: Also known as instance variables.

Static variables: Also known as class variables.


4. Two variables are stored in different locations:

Member variables: Objects stored in heap memory, so they are also called object-specific data.

Static variables: stored in the static area of the method area (shared data area), so is also called the shared data of the class.


[Javase Learning Note the difference between a member variable and a static variable]-8.2

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.