Final keyword in Java – Final variables, methods, and classes
Final is a keyword in Java that can be applied to variables, methods, and classes to restrict their behavior. This post provides an overview of the behavior of variables, methods, and classes in Java when a final keyword is applied. Let’s discuss each one of them in detail:
1. Final variable
We cannot change the value of a final variable once it is initialized. A final variable is different from a constant as the value of a final variable is not necessarily known at compile time.
A final variable can only be initialized once, either via an initializer or an assignment statement. If a final variable is not initialized during declaration, it must be initialized inside the class’s constructor in which it is declared. Such a variable is also called a blank final variable. Any attempt to set a blank final variable outside the constructor will result in a compilation error.
Similarly, if a static final variable is not initialized at the declaration, then it must be initialized inside the static initializer block of the class in which it is declared. Such a variable is called a blank static final variable. Any attempt to set a blank static final variable outside the static initializer will result in a compilation error.
Consider the following code. Here any attempt to reassign x, y, or z will result in a compilation error.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Point { public final int x = 1; // final variable public final int y; // blank final variable public static final int z; // blank static final variable // Constructor public Point() { y = 2; } // Static initializer block static { z = 3; } } class Main { public static void main(String[] args) { Point pt = new Point(); System.out.println("(" + pt.x + "," + pt.y + "," + pt.z + ")"); } } |
Output:
(1,2,3)
If a final variable holds a reference to an object, then the object’s components may be changed by operations on the object. Still, the variable will always refer to the same object.
To illustrate that finality doesn’t guarantee immutability, consider the following code, where we have replaced the x, y, z variables with a single Position object with three properties x, y, and z. Now Position object cannot be assigned to, but the three properties can unless they are themselves final.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
class Position { // non-final fields public int x, y, z; // Constructor public Position(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } @Override public String toString() { return "(" + x + "," + y + "," + z + ")"; } } class Point { // pos is final public final Position pos; // Constructor public Point(int x, int y, int z) { pos = new Position(x, y, z); } public void modify() { // uncommenting the following line results in compilation error // pos = new Position(10, 20, 30); // however, this is allowed pos.x = 10; pos.y = 20; pos.z = 30; } } class Main { public static void main(String[] args) { Point pt = new Point(1, 2, 3); System.out.println(pt.pos); pt.modify(); System.out.println(pt.pos); } } |
Output:
(1,2,3)
(10,20,30)
2. Final class
If a class is declared as final in Java, then it cannot be extended. A final class is used for secure and efficient code. Several classes in the Java standard library are final. For instance, the System class in the java.lang package is final. The String class is also is final in Java.
To illustrate a final class’s behavior, consider the following code, which throws a compilation error since class B tries to extend a final class A.
|
1 2 3 4 5 6 7 |
final class A {} class B extends A {} // forbidden class Main { public static void main(String[] args) {} } |
Output (Compilation error):
Main.java:3: error: cannot inherit from final A
class B extends A { // error!
^
3. Final method
If a method is declared as final in Java, then it cannot be overridden by any subclass of the class in which it is declared. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the class’s method or consistency.
To illustrate the behavior of a final method, consider the following code, which throws a compilation error:
|
1 2 3 4 5 6 7 8 9 10 11 |
class A { public final void fun() {} } class B extends A { public void fun() {} // forbidden } class Main { public static void main(String[] args) {} } |
Output (Compilation error):
Main.java:8: error: fun() in B cannot override fun() in A
public void fun() {
^
overridden method is final
That’s all about the final keyword in Java.
Reference: Final (Java) – Wikipedia
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)