Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

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

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

2. final and static: A Brief Overview

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.

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.

3. The Order of static and final

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.

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.

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.

4. When to Use static final

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.

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:

  • 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

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.

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.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.