Introduction:
The final keyword is a non-access modifier in Java that is used with variables, methods, and classes. It is used in different contexts and has a few specific uses that we will discuss in this article.
Final Variables:
When a variable is declared with the final keyword, its value can't be modified, making it a constant. Final variables must be initialized when declared, and if the variable is a reference, it cannot be re-bound to reference another object. However, the internal state of the object pointed to by that reference variable can be changed, such as adding or removing elements from a final array or collection. It's good practice to represent final variables in uppercase, with underscores to separate words.
Example:
final int THRESHOLD = 5; // final variable final int THRESHOLD; // blank final variable static final double PI = 3.141592653589793; // final static variable PI static final double PI; // blank final static variable
Initializing a Final Variable:
A final variable must be initialized, either via an initializer or an assignment statement. There are three ways to initialize a final variable:
- Initialize it when it is declared (the most common way).
- Initialize a blank final variable inside an instance-initializer block or inside the constructor. If a class has more than one constructor, it must be initialized in all of them.
- Initialize a blank final static variable inside a static block.
Example: final int THRESHOLD = 5; // initialized when declared final int CAPACITY; // blank final variable final int MINIMUM; // another blank final variable static final double PI = 3.141592653589793; // initialized when declared static final double EULERCONSTANT; // blank final static variable
// instance initializer block for initializing CAPACITY { CAPACITY = 25; }
// static initializer block for initializing EULERCONSTANT static { EULERCONSTANT = 2.3; }
// constructor for initializing MINIMUM public ClassName() { MINIMUM = -1; }
Observation 1: When to Use Final Variables:
The only difference between a normal variable and a final variable is that we can re-assign the value to a normal variable but we cannot change the value of a final variable once assigned. Hence final variables must be used only for the values that we want to remain constant throughout the execution of the program.
Observation 2: Reference Final Variable:
When a final variable is a reference to an object, it's called a reference final variable. The internal state of the object pointed to by that reference variable can be changed. This property of the final keyword is called non-transitivity. This applies to arrays as well because arrays are objects in Java. Arrays with the final keyword are called final arrays.
Example:
final StringBuilder sb = new StringBuilder("Geeks"); // final reference variable System.out.println(sb); // prints "Geeks" sb.append("ForGeeks"); // changes the internal state of the object pointed to by sb System.out.println(sb); // prints "GeeksForGeeks"
Note that reassigning a final variable will throw a compile-time error.
Example:
static final int CAPACITY = 4; // declared and initialized CAPACITY = 5; // reassigning will throw a compile-time error
Conclusion:
The final keyword is a useful tool in Java for creating constants and preventing modifications to variables, methods, and classes. It's important to initialize final variables properly and understand their limitations and properties, such as reference final variables and final arrays.