Sitemap
berkegenc

berkegenc

software engineer

Java’ Final Keyword —- Java Basics

4 min readSep 23, 2025

--

Note: While writing this article we assumed that we have basic knowledge about Java language

The final keyword is an important keyword used in the following declarations:

  1. final variables
  2. final classes
  3. final methods

Final Variables

As remembered, Java has 3 types of variables: local variables, member variables, parameter variables. When a variable is declared as final, it means it can only be assigned a value once, and you cannot change it afterward.

Some variables can have a final effect under certain conditions even if the final keyword is not used. This concept is called effectively final. This concept will be covered later.

Final Variable Rules

Local Variables: A final local variable can be assigned a value only once within its scope, either by initialization or assignment.

Parameter Variables: A method’s parameter variable can be final. In this case, the value of that parameter variable cannot be changed within the method.

Get berkegenc’s stories in your inbox

Join Medium for free to get updates from this writer.

Member Variables: A member variable can be declared as final. Final member variables are NOT given default values.

Note: For class member variables, access modifier, static keyword, and final keyword are at the same syntax level, so they can be written in any order. However, for readability/comprehensibility, it is appropriate to write them in the following order in declarations: [access modifier] [static] [final]

Assigning Values to Final Member Variables

Non-Static Final Member Variables: A class’s non-static and final declared member variable can be assigned a value in only one of the following three places:

  • At declaration point (initialization)
  • Inside all constructors
  • Inside non-static initializer

Static Final Member Variables: A class’s static and final declared member variable can be assigned a value in only one of the following 2 places:

  • At declaration point (initialization)
  • Inside static initializer

Note: If a non-static and final declared member variable is not assigned a value outside the constructor, it must be assigned a value in all constructors. Otherwise, an error occurs.

Constant Expressions and Naming Convention

It is not mandatory to assign a value to a final variable with a constant expression. When a value is assigned to a class’s static and final declared member variable with a constant expression, that member variable can be used as a constant expression.

For a class’s public, static, and final declared member variable (especially when assigned with a constant expression), as a convention, it should be named in all uppercase letters, and if it consists of multiple words, the words should be separated by underscore characters.

Examples

Example 1: Basic Final Usage

public class FinalBasics {
// Static final constants - naming convention
public static final int MAX_SIZE = 100;
public static final String DEFAULT_NAME = "Unknown";
public static final double PI_VALUE = 3.14159;

// Non-static final member variable
private final String id;
private final int value;

// Constructor must initialize all final member variables
public FinalBasics(String id, int value) {
this.id = id; // Must be assigned
this.value = value; // Must be assigned
}

// Alternative constructor
public FinalBasics(String id) {
this.id = id;
this.value = 0; // Must be assigned in all constructors
}

public void demonstrateLocalFinal() {
final int localVar = 10; // Initialized at declaration
final int anotherVar; // Will be assigned later

anotherVar = 20; // First and only assignment

// localVar = 15; // ERROR: Cannot reassign
// anotherVar = 25; // ERROR: Cannot reassign

System.out.println("Local variables: " + localVar + ", " + anotherVar);
}

public void methodWithFinalParameter(final int param) {
// param = 10; // ERROR: Cannot modify final parameter
System.out.println("Parameter: " + param);
}

public String getId() { return id; }
public int getValue() { return value; }
}

Best Practices with Final

If making a member variable final does not change the code algorithmically and does not cause syntax errors, the programmer should always prefer to write their code as final.

When to Use Final for Member Variables

public class BestPracticeExample {
// Always prefer final when possible
private final String name; // Good: immutable after construction
private final int id; // Good: immutable after construction
private final List<String> items; // Good: reference is final

// Only non-final when mutation is required
private int counter; // OK: needs to change
private double balance; // OK: needs to change

public BestPracticeExample(String name, int id) {
this.name = name;
this.id = id;
this.items = new ArrayList<>();
}

// Final reference doesn't prevent collection modification
public void addItem(String item) {
items.add(item); // OK: modifying contents, not reference
}

// This would be impossible with final
// public void setItems(List<String> newItems) {
// this.items = newItems; // ERROR: Cannot reassign final field
// }
}

Summary

  • Final variables can only be assigned once
  • Final member variables must be initialized at declaration, in constructors, or in initializer blocks
  • Static final constants should follow ALL_CAPS naming convention

--

--