Difference Between “final static” and “static final” – “final static” 和 “static final&#8221 之间的区别;

最后修改: 2023年 11月 5日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

The keywords final and static in Java have different meanings, but they can be used together to declare a constant variable.

Java 中的关键字 finalstatic 含义不同,但它们可以一起用于声明一个常量变量。

In this tutorial, we’ll discuss the difference between final static and static final.

在本教程中,我们将讨论 final staticstatic final 的区别。

2. final and static: A Brief Overview

2.最终静态:简要概述

The final keyword indicates that a variable cannot be reassigned once it’s been initialized. This is useful for preventing accidental changes to important variables.

final 关键字表示 变量一旦初始化,就不能再分配。这对于防止意外更改重要变量非常有用。

The static keyword indicates that a variable belongs to the class itself rather than to any particular instance of the class. This means that there is only one copy of the variable, which is shared by all instances of the class.

static 关键字表示变量属于类本身,而不属于类的任何特定实例。这意味着 该变量只有一个副本,由类的所有实例共享。

3. The Order of static and final

3.静态和最终的顺序

When we combine the final and static keywords, we create a variable that is both constant and class-level. This means that it cannot be reassigned, and it’s shared by all instances of the class.

当我们结合使用 finalstatic 关键字时,我们将创建一个既是常量又是类级的变量。这意味着它不能被重新赋值,并且为类的所有实例所共享。

It’s essential to understand the order of static and final when declaring constant variables. While it’s widely accepted to use both static final and final static, some coding standards and tools such as Sonar and Checkstyle may enforce the order static final. Therefore, it’s a good practice to follow the static final order to ensure code consistency.

在声明常量变量时,理解 staticfinal 的顺序至关重要。虽然同时使用 static finalfinal static 已被广泛接受,但一些编码标准和工具(如 Sonar 和 Checkstyle)可能会强制执行 static final 的顺序。因此,遵循 static final 的顺序是确保代码一致性的良好做法。

Let’s illustrate the creation of constant class-level variables:

让我们来说明一下如何创建类级常量变量:

public class MyClass { 
    public final static int MY_CONSTANT = 10; 
    public static final int MY_OTHER_CONSTANT = 20;
}

We can access the MY_CONSTANT and MY_OTHER_CONSTANT variables from any instance of the MyClass class or from outside of the class using the MyClass class name. However, we cannot reassign either variable.

我们可以从 MyClass 类的任何实例访问 MY_CONSTANTMY_OTHER_CONSTANT 变量,也可以使用 MyClass 类名从类外访问 MyClass 变量。但是,我们不能重新分配这两个变量。

4. When to Use static final

4. 何时使用静态最终

We should use final static or static final to declare any variable that we want to keep constant. This includes variables that contain configuration settings, mathematical constants, or other values that shouldn’t be changed.

我们应该使用 final static 或 static final 来声明任何我们希望保持不变的变量。这包括包含配置设置、数学常量或其他不应更改的值的变量。

Using static final variables can help to improve the readability and maintainability of our code. It also helps to prevent errors by preventing accidental changes to important variables.

使用 静态最后变量有助于提高代码的可读性和可维护性。它还能防止对重要变量的意外更改,从而避免错误的发生。

Examples of when we should use static final:

使用 static final 的示例:</em

  • To declare configuration settings, such as the database connection string or the path to a log file
  • To declare mathematical constants, such as π or e

5. Conclusion

5.结论

The final static and static final keywords in Java are used to declare constant, class-level variables. This is useful for preventing accidental changes to important variables and improving the readability and maintainability of our code.

Java 中的 final static 和 static final 关键字用于声明常量、类级变量。这对于防止意外更改重要变量以及提高代码的可读性和可维护性非常有用。

In this article, we learned that the sequence of static and final modifiers is inconsequential. However, in practical applications, it’s advisable to employ the order static final.

在本文中,我们了解到 staticfinal 修饰符的顺序并不重要。但是,在实际应用中,我们建议使用 static final. 的顺序。