Home  >  Article  >  Java  >  What is the difference between static variables and instance variables in Java?

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

青灯夜游
青灯夜游Original
2019-11-16 16:13:035669browse

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

Static variables belong to the class level, while instance variables belong to the object level.

There are two main differences between static variables and instance variables:

1. Different storage locations

Class variables exist in In the method area, instance variables exist in the heap memory as the object of the object is created.

2. Different life cycles

Class variables have the longest life cycle. They are loaded as the class is loaded and disappear as the class disappears. Instance variables disappear as the object disappears. .

Notes on static usage:

1. Static methods can only access static members (including member variables and member methods), but cannot access non-static members or methods.

Non-static methods can access static or non-static methods or members.

2. This and super keywords cannot appear in static methods.

Because static precedes the existence of objects, this and super keywords cannot appear.

3. The main function is static.

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!

Statement:
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
Previous article:What is the role of java?Next article:What is the role of java?