Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

Why we use static final in Java for constants

Developers don't use the const keyword in Java to mark fields as constants. Instead, they daisy chain the keywords static final in Java to create an arguably global variable with an unchangeable value.

While the const word is reserved in Java, it's unimplemented and any attempt to use it in code will trigger a compile time error. Developers that come to Java from other languages will find the use of the terms static final in Java instead of the const keyword to be unintuitive. However, there's an object-oriented programming reason why one would implement the language this way.

The difference between static and final in Java

A developer creates Java classes such as the Person class or the BankAccount class from which they make instances. Think of it this way, a class is like a cookie cutter while an individual instance is like a cookie.

If a developer wants a field never to change after it's been assigned a value, mark that field as final. For example, my bank account number will never change after it's been created, and neither will yours. Obviously, your bank account number is different from mine, so they're not the same for every instance of the BankAccount class. But for each individual instance, the account ID will never change.

Java const keyword
Use of the keyword const in Java will trigger a compile error

Hence it would be somewhat inaccurate to call the account ID constant, because it changes from one account to another. However, this is true for any individual account. It wouldn't be philosophically sound to use the word constant -- or the Java keyword const -- to describe this field.

Java's static vs. final keywords

Now let's look at the Java keyword static. A field marked static is the same for every instance of a class. A bank account might have a static variable that represents the interest rate, for example. When the interest rate changes, it changes for every bank account instance. If tomorrow the interest rate goes from 1% to 2%, it will affect everyone. It's like a global variable that every instance of the class can access. But a static variable is not constant because it can be changed at any time.

Now imagine that a developer wants a value that can't be changed and is the same for every instance of the class. A developer needs to combine the keywords static final to achieve this in Java. The static keyword means the value is the same for every instance of the class. The final keyword means once the variable is assigned a value it can never be changed. The combination of static final in Java is how to create a constant value.

Example of static final in Java

Here's a static final example to further demonstrate the point.

The following Account class has three properties:

  • accountId of type int marked as final;
  • interestRate of type double marked as static; and
  • a final static variable named odLimit of type int for the overdraft limit.
class Account {
	
  public Account(int id) { accountId = id; }
	
  //const int dne;
  final int accountId;
  static double rate = 1.5;
  static final int odLimit = 1000;
	
}

Even though the accountId is marked final, each individual account can have its own unique value. The only rule is that once the value is assigned, it can never be changed.

Account first = new Account(123);
Account second = new Account(456);

// The following prints 123
System.out.println(first.accountId);
// The following prints 456
System.out.println(second.accountId);

// Next line triggers a compile error
// first.accountId = 789;

With the non-final static variable, any instance can change it, but if the value changes, it affects every instance.

first.rate=2.5;
second.interestRate=3.5;

// Both lines print out 3.5
System.out.println(first.interestRate);
System.out.println(second.interestRate);

Finally, with the static final variable, it's both the same for each class and it can't be changed after it's initialized.

// Both lines print out 1000
System.out.println(first.odLimit);
System.out.println(second.odLimit);
// Next line triggers a compile error
// first.odLimit = 500;

No need for a Java const implementation

The terms static and final in Java have distinct meanings. The final keyword implies something cannot be changed. The static keyword implies class-level scope. When you combine static final in Java, you create a variable that is global to the class and impossible to change. Developers from other platforms would consider this creation to be equivalent to a global, constant variable. The static final keywords used together eliminate the need for an implementation of Java's const keyword.

You can find the code used in this static final variable example on GitHub.

View All Videos

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

SearchAppArchitecture

SearchSoftwareQuality

SearchCloudComputing

SearchSecurity

SearchAWS

Close