Differences between Java static variables and instance variables

Source: Internet
Author: User

Syntax definition difference: the static keyword must be added before the static variable, but not before the instance variable.
Differences during program running: instance variables belong to the attributes of an object. You must create an instance object. Only instance variables in the instance can be allocated space to use this instance variable. Static variables do not belong to a certain instance object but belong to a class, so they are also called class variables. As long as the program loads the class bytecode, no instance object needs to be created, static variables are allocated space, and static variables can be used. In short, instance variables can be used only after an object is created. Static variables can be referenced directly by class names.
For example, for the following program, no matter how many instance objects are created, only one staticVar variable is always assigned, and each time an instance object is created, this staticVar will add 1; however, each time an instance object is created, an instanceVar will be assigned, that is, multiple instanceVar may be allocated, and each instanceVar value is added only once. Public class VariantTest {public static int staticVar = 0; public int instanceVar = 0; public VariantTest () {staticVar; instanceVar; System. out. println ("staticVar =" staticVar ", instanceVar =" instanceVar);} Article 2:
There are two types of java class member variables:
One is a variable modified by the static keyword, called a class variable or a static variable.
The other is an instance variable without static modification.

The class has only one static variable in the memory. the Java Virtual Machine allocates memory for the static variable during the class loading process. The static variable is located in the method area and is shared by all instances of the class. Static variables can be accessed directly through the class name. the life cycle of a static variable depends 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. The instance variable is located in the heap area, and its life cycle depends on the life cycle of the instance.

Public class Temp {
Int t; // instance variable
Public static void main (String args []) {
Int t = 1; // local variable
System. out. println ( T); // Print local variables
Temp a = new Temp (); // create an instance
System. out. println ( A. t); // Access instance variables through instances
}
}
Result:
1
0 ( Member variables have default values, while local variables do not)

Change the code
Public class Temp {
Static int t; // class variable
Public static void main (String args []) {
System. out. println ( T); // Print class variables
Int t = 1; // local variable
System. out. println ( T); // Print local variables
Temp a = new Temp (); // create an instance
System. out. println ( A. t); // Access instance variables through instances
}
}
The result is
0
1
0

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.