Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 19:16:26 09月24日 Update

This article mainly explains "what is the difference between static variables and instance variables in Java development". The content in the article is simple and clear, and it is easy to learn and understand. Please follow Xiaobian's train of thought to study and learn "what is the difference between static variables and instance variables in Java development".

The difference when the program is running: the instance variable belongs to the properties of an object, and the instance object must be created before the instance variable can be allocated space to use this instance variable. Static variables do not belong to an instance object, but belong to a class, so they are also called class variables. as long as the program loads the bytecode of the class and does not have to create any instance objects, the static variable will be allocated space, and the static variable can be used. In short, instance variables must create an object before they can be used through this object, while static variables can be referenced directly by the class name.

For example, for the following program, only one staticVar variable is always assigned no matter how many instance objects are created, and the staticVar is incremented by 1 for each instance object created; however, for each instance object created, an instanceVar is allocated, that is, multiple instanceVar may be allocated, and the value of each instanceVar is only incremented once.

Thank you for reading, the above is the content of "what is the difference between static variables and instance variables in Java development?" after the study of this article, I believe you have a deeper understanding of what is the difference between static variables and instance variables in Java development. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Variables instances objects static allocation development programs learning content space no can be passed multiple bytes that is attributes ideas situations articles more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Apple vpn OPPO Reno Docker