Abstract: static keywords and final keywords are the core of the Java language, and it is very important to have a deep understanding of their functions.
This article is shared from the Huawei Cloud Community " Java: static keyword and final keyword ", the original author: Tang Li.
The static keyword and final keyword are the core of the Java language, and it is very important to have a deep understanding of their functions.
Static variable
No matter how big a program you write, you will encounter the static keyword, like this, in the main() method:
In short, the fields with the static keyword belong to the class, they are class methods and variables.
When you learn object-oriented programming in class or in books, you may encounter something similar to a blueprint. Let's make an analogy. There is a blueprint of a house (this blueprint can build many houses, just like you can use this class to create many objects/instances). On this blueprint, there may be information describing the blueprint itself instead of the house. Therefore, if there is a static variable in a class, the variable will hold information about the class itself. I hope this analogy can make it easier for you to understand, if not, take a look at the following example:
Variables without the keyword static are called instance variables, so the userName variable is regarded as an instance variable of the User class.
Now, our main() method is:
Note the difference between setting the instance variable "user name" and the description of the static variable. When we set the instance variable "username", we first create an object/instance of the User class, and then set its userName variable. In order to set the static variable'description', we set it on the class itself via'User.setDescription ("My User Class");'. Therefore, by setting the "description" variable to static, there is only one such field per class, while there are many "userName" fields (one for each created object).
How does it work? In fact, one practical use of class variables is to keep counting the number of instances of the class we have. E.g:
We added a'count' static variable in the User class and incremented it in the constructor. Every time we create an instance of the User class, there will be a variable to keep track of how many instances have been created.
The last thing to note is that static variables are not initialized with values, they are "automatically" initialized with default values, which are:
primitive integers(long, short etc): 0
primitive floating points(float, double): 0.0
boolean: false
object references: null
0 primitive floating points (float,double) : 0.0 boolean: false object references: null
Static method
Static methods belong to class methods. One important thing to note is that static methods cannot access instance variables; they can only access other static fields. If you try to access a static field through an instance method, don't worry, the compiler will alert you with an error:
non-static variable this cannot be referenced from a static context
This is because the instance variable does not exist before the instance is initialized; and the static variable is created when it is declared in the class. On the other hand, instance methods can access static variables.
Accessibility: A static field or method can only be marked as private when it is only used within the class. If you want to use them outside of the class, then they must be marked as protected or public.
final keyword
Simply put, the final keyword is the version of Java that marks the variable as a constant. The Final keyword prevents variables from being reassigned to a different value, so once a value is assigned, it cannot be reassigned to other variables. When you declare a variable and give it a final label, it must also be initialized.
Coding standards: In Java, like many other languages, constants are always all uppercase. For example, π (representing the ratio of the circumference to its diameter), if you write PI, it will cause the following error:
java: cannot assign a value to final variable PI
In the Math class, PI variables are marked with the final keyword to become a constant, and variables with the final keyword cannot be reassigned. Use the final keyword on methods to prevent them from being overridden, and use the final keyword on the class level to prevent the class from having subclasses (other classes cannot inherit from the class with the final keyword).
The above is the whole content of this article, I hope it will help you.
Original link: https://suprun-anton6.medium.com/java-static-and-final-keywords-66a3f8b3db8a
Click to follow and learn about Huawei Cloud's fresh technology for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。