Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

2 Comments
Oldest
Newest