Home > Java > Javagetting Started > body text

What is the difference between static variables and instance variables in Java

王林
Release: 2020-07-16 16:58:04
forward
3306 people have browsed it

What is the difference between static variables and instance variables in Java

Difference analysis:

(Recommended tutorial: java introductory tutorial)

1. Syntax difference

Static variables need to be modified with the static keyword, but instance variables do not.

2. The difference when the program is running

Static variables are subordinate to classes, and instance variables are subordinate to objects.

Instance variables must create an instance object, and the instance variables in it will be allocated space before this instance variable can be used; static variables are class variables. As long as the program loads the bytecode of the class, the static variables will Once the space is allocated, it can be used.

Summary: Instance variables must be used through this object after creating an object. Static variables can be referenced directly using the class name.

(Video tutorial recommendation: java video tutorial)

Note: The use of (static) static variables also has limitations. A static method cannot call a class Non-static methods and variables, static-modified variables have only one memory space in the memory after the class is loaded, and can be shared by all instance objects of a class.

The above is the detailed content of What is the difference between static variables and instance variables in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template