The final
keyword in Java is a versatile keyword that can be used to define constants, prevent inheritance, and ensure immutability. Its usage can be applied to variables, methods, and classes, each serving a specific purpose in enhancing the security and robustness of Java applications.
final
with VariablesWhen final
is used with variables, it means that the variable’s value cannot be changed once it has been initialized. This is often used to create constants.
Final instance variables must be initialized either at the point of declaration or within the constructor.
public class Example {
final int MAX_VALUE;
// Constructor
public Example(int maxValue) {
this.MAX_VALUE = maxValue;
}
public static void main(String[] args) {
Example example = new Example(100);
// example.MAX_VALUE = 200; // Compilation error: cannot assign a value to final variable MAX_VALUE
}
}
Final local variables must be initialized at the time of declaration or within the block where they are defined.
public class Example {
public void display() {
final int age = 25;
// age = 30; // Compilation error: cannot assign a value to final variable age
System.out.println("Age: " + age);
}
}
Final static variables are also known as constants. They are typically declared using uppercase letters.
public class Constants {
public static final double PI = 3.14159;
public static void main(String[] args) {
System.out.println("Value of PI: " + PI);
}
}
final
with MethodsWhen final
is used with a method, it means that the method cannot be overridden by subclasses. This is particularly useful when you want to ensure that the implementation of a method remains unchanged.
public class Parent {
public final void display() {
System.out.println("This is a final method.");
}
}
public class Child extends Parent {
// This will cause a compilation error
// public void display() {
// System.out.println("Trying to override a final method.");
// }
}
final
with ClassesWhen final
is used with a class, it means that the class cannot be subclassed. This is useful when you want to prevent inheritance for security or design reasons.
public final class ImmutableClass {
private final int value;
public ImmutableClass(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
// This will cause a compilation error
// public class Subclass extends ImmutableClass {
// public Subclass(int value) {
// super(value);
// }
// }
final
final
with static variables to create constants that cannot be changed.final
with class attributes to create immutable objects.final
with classes and methods to prevent unintended inheritance and method overriding.Creating immutable classes often involves using final
for class and instance variables. Immutable objects are objects whose state cannot be changed after they are created.
public final class ImmutablePerson {
private final String name;
private final int age;
public ImmutablePerson(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Main {
public static void main(String[] args) {
ImmutablePerson person = new ImmutablePerson("John", 30);
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
}
}