Abstract: Static and final keywords are the core of the Java language. It is very important to understand their functions deeply.

This document is shared with Huawei cloud community Java: Static keyword and Final keyword. The original article is written by Tang Li.

The static and final keywords are at the heart of the Java language, and it is important to understand their capabilities in depth.

A static variable

No matter how big a program you write, you’ll encounter the static keyword, like this, in the main() method:

In short, fields with the static keyword belong to classes; they are class methods and variables.

When you study object-oriented programming in class or in a book, you may come across something like a blueprint. As an analogy, if you have a blueprint for a house (this blueprint can build many houses, just as you can create many objects/instances with this class), on this blueprint there may be information that describes the blueprint itself rather than the house. Therefore, if a class has a static variable, that variable will hold information about the class itself. I hope this analogy makes it easier for you, if not, take a look at the following example:

Variables without the keyword static are called instance variables, so the userName variable is considered to be an instance variable of the User class.

Now, our main() method is:

Note the difference between setting instance variable “username” and static variable description. When we set the instance variable “userName”, we first create an object/instance of the User class and then set its userName variable. To set the static variable ‘description’, we use ‘user.setDescription’ (‘ My User Class ‘); ‘Set it on the class itself. So, by setting the “description” variable to static, each class has only one such field, and there are many “userName” fields (one for each object created).

How does it work? In fact, one practical use of class variables is to keep counting the number of instances of classes we have. Such as:

We add a ‘count’ static variable to the User class and increment it in the constructor. Every time we create an instance of the User class, we have a variable that keeps track of how many instances we have created.

Finally, note 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

A static method

Static methods are 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
Copy the code

This is because the instance variable does not exist until the instance is initialized; Static variables are created when they are declared in a class. Instance methods, on the other hand, can access static variables.

Accessibility: Static fields or methods can only be marked private if they are used only within a class. If they are to be used outside the class, they must be marked protected or public.

The final keyword

Simply put, the final keyword is the version of Java that marks variables as constants. The Final keyword prevents a variable from being reassigned to a different value, so once a value is assigned, it cannot be reassigned to another variable. When you declare a variable and attach a final label to it, it must also be initialized.

Coding specification: In Java, as in many other languages, constants are always capitalized. For example, PI (the ratio of the circumference of a circle to its diameter) will cause the following error if you write PI:

java: cannot assign a value to final variable PI
Copy the code

In the Math class, PI variables are marked as constants with the final keyword, and variables with the final keyword cannot be reassigned. Using the final keyword on Methods prevents them from being overridden, and using the final keyword at the class level prevents that class from having subclasses (other classes cannot inherit from classes that have the final keyword).

The above is the content of this article, I hope to help you.

Original link: suprun-anton6.medium.com/java-static…

Click to follow, the first time to learn about Huawei cloud fresh technology ~