Explain Codes LogoExplain Codes Logo

Difference between final static and static final

java
best-practices
code-quality
static-final
Alex KataevbyAlex Kataev·Feb 4, 2025
TLDR
No difference in behavior: `final static` and `static final` both create a **class-level unmodifiable constant**. However, **`static final`** is the conventional order, aligning with the practice of calling out the scope (`static` - belongs to the class) before the immutability (`final` - cannot change).

Example:
```java
public class Config {
    public static final double PI = 3.14159;  // Like a circle, the value of PI never ends!
}

The convention of static final enhances readability and clarity in code.

Unfold the commonality and convention

In Java, both final static and static final make a variable immutable, meaning the value can't be modified once it's initialized. While they serve the same purpose, there's a preferred order in the Java programming community.

  • static final:
    • static implies that the variable is shared among all instances of the class.
    • final means that the assigned value of the variable can never be altered.

By putting static before final, clarity is improved as it communicates that the variable belongs to the class, before revealing its constant nature. This order is preferred by many, and tools like SonarQube even flag final static as a code smell, advocating instead for static final for better readability.

Visibility & order of modifiers

The access modifier (public, protected, private) determines the visibility of a static final or final static variable. Although this does not affect how static and final work, it is essential for encapsulation and effective OOP design.

Here's an example:

public class Constants { public static final int MAX_USERS = 50; // More public than social media, accessible anywhere! private static final String API_KEY = "..."; // Like a secret diary, enclosed within the class. }

As a good practice, if a constant represents universal knowledge, like the maximum number of users in your application, it should be public. If it's sensitive or specific to class implementation, like an API key, it should be private to secure your implementation details.

Syntax pitfalls and practical use-cases

When working with constants in Java, it's important to understand their practical implications and avoid common pitfalls:

  • Initialization: A static final variable should be initialized either when it's declared or within a static initialization block.

  • Memory Management: By making a variable static final, we only allocate memory once. This is kinder to your memory than an overly attached variable allocating memory per object.

  • Thread Safety: static final variables are thread-safe during initialization. No more "race-conditions", they wait their turn!

The tricky part is if you try to modify a static final variable. This will lead straight to an error. Also, remember to handle serialization with care, final may sound unchangeable, but serialization sometimes begs to differ.

Code Quality boosters

Following the conventional modifier order not only encourages readability but also improves overall code equality. Consider linters, formatters, sonar guns... okay, let's not get carried away. But these can be built into your process.

  • Sweet consistency: Keep your modifiers in order. It’s like matching socks, keeps everything neat and confusion-free.

  • Automated checks: No one enjoys manual labor. So, use code analysis tools to highlight deviations and enforce best practices.

  • IDEs are your friends: IDEs like IntelliJ IDEA and Eclipse will highlight and suggest rearranging modifiers. Coding can be tough, so every little helper counts.