Static variable and instance variable difference

Source: Internet
Author: User

There are two types of member variables for Java classes:

1. Static variables, variables modified by static, also called class variables.

2. Instance variable, no static modified variable.

Difference:

static variables are shared, in the entire JVM, only there is one, and the member variable is every instance unique. For use by the instance itself.

Example:

A student class: Age, Name, gender is an instance variable, because each person's age, name may be different;

Tuition can be class variables, and tuition fees are the same for each student. (except for special)

Application Scenarios:

When an object is shared by all instances of this class, use the class variable
If the value of each instance may be different, use an instance variable.


static variables are based on soundMing, the function is different, the general static variable declaration isIn accordance with this example, an externalsomeOf the information, such static variables usually soundPublic , in order to facilitate andoutsideinteraction.

and the usage declared as private isis very miscellaneous, the most common is that some concurrency within the classcontrol, as well as the constant within the classvariables, as well as a particularly important single casemode, all used is the PRIthe static change of vatevolume.

at the same time as static declaration, the declaration is final is a constant, and C is the same as const, forsome flag bits, and a fixed display string, and so on


The difference between the programs:

An instance variable is a property of an object, and an instance object must be created where the instance variable is allocated space before the instance variable can be used.

Static variables are not part of an instance object, but belong to a class, so also known as class variables, as long as the program loads the class's bytecode, without creating any instance objects, static variables will be allocated space, static variables can be used.

In summary, an instance variable must be created before it can be used by the object. Static variables can be referenced directly by using the class name;


Differences in running results:

The difference between a class variable and an instance variable is that the class variable is common to all objects, where one object changes its value, and the other object gets the changed result;

The instance variable is a private object, and an object changes its value without affecting other objects;

Differences in Memory:

The static variables of the class are only one in memory, and the Java Virtual machine allocates memory for static variables during the loading of the class, and the static variables are in the method area, all of the classes instance sharing. Static variables can be accessed directly from the class name, depending on the life cycle of the class.
The instance variable depends on the instance of the class. Each time an instance is created, the Java Virtual machine allocates memory for the instance variable once, and the instance variable is in the heap, whose life cycle depends on the life cycle of the instance.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Static variable and instance variable difference

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.