• 周五. 12月 9th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Explain the function of static keyword and final keyword in Java

[db:作者]

1月 6, 2022

Abstract :static Key words and final Keywords are Java The core of language , It’s important to have a deep understanding of their functions .

This article is shared from Huawei cloud community 《Java: static The key words and final keyword 》, Original author : Tangli .

static Key words and final Keywords are Java The core of language , It’s important to have a deep understanding of their functions .

Static variables

No matter how big a program you write , Will meet static keyword , like this , stay main() In the method :

In short , with static Keyword fields belong to class , They are class methods and variables .

When you learn object-oriented programming in class or in books , You might come across something like a blueprint . Let’s make an analogy , There’s a blueprint for the house ( This blueprint can build a lot of houses , It’s like you can create a lot of objects with this class / The example is the same ) , On this blueprint , There may be information describing the blueprint itself rather than the house . therefore , If a class has a static variable , Then the variable will hold information about the class itself . I hope this analogy makes it easier for you to understand , If not , Take a look at the following example :

No keywords static Is called instance variable , therefore userName The variable is considered to be User Class instance variables .

Now? , our main() The method is :

Pay attention to setting instance variables “ user name ” And the difference between static variable description . When we set instance variables “ user name ” when , Let’s first create User Class object / example , And then set its userName Variable . To set static variables ‘ description’, We go through ‘ User.setDescription (“ My User Class”) ;’ Set it on the class itself . therefore , By way of “ description” The variable is set to static, There is only one such field per class , And there are many “ userName” Field ( Each created object has one ).

How it works ? actually , A practical use of class variables is to constantly count the number of instances of the class we have . for example :

We are User Class added a ‘ count’ Static variables , And increment it in the constructor . Every time we create User class , There will be a variable to track how many instances are created .

Last but not least , Static variables are not initialized with values , They use default values “ Automatically ” The initialization of the , The default value is :

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 methods

Static methods belong to class methods . An important thing to pay attention to is , 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 about , 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 ; Static variables are created when declared in a class . On the other hand , Instance methods can access static variables .

Accessibility : Only when a static field or method is used only within a class , To mark it as private. If you want to use them outside of classes , Then they have to be marked as protected or public.

final keyword

Simply speaking ,final The key word is Java The version that marks a variable as a constant .Final Keywords prevent variables from being reassigned to a different value , So once a value is assigned , You can’t reassign it to other variables . When you declare a variable and tag it with the final tag , It also has to be initialized .

Coding standards : stay Java in , Like many other languages , Constants are always all uppercase . for instance , π ( Represents the ratio of the circumference of a circle to its diameter ), If you write PI Will result in the following error :

java: cannot assign a value to final variable PI

stay Math Class ,PI Variables are marked as final Keywords become constants , With final Keyword variables cannot be reassigned . stay methods Upper use final Keywords prevent them from being rewritten , And use… At the class level final Keywords prevent the class from having subclasses ( Other classes cannot derive from having final Keyword class inheritance ).

The above is the whole content of this paper , I hope it helped you .

Link to the original text :https://suprun-anton6.medium.com/java-static-and-final-keywords-66a3f8b3db8a

 

Click to follow , The first time to learn about Huawei’s new cloud technology ~

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注