final Keyword in Java
The final
Keyword in Java
final
Keyword in JavaThe final
keyword in Java is used to restrict the usage of variables, methods, and classes. It serves as a form of protection, ensuring that certain aspects of your code remain unchanged, thereby improving the safety and predictability of your program. Understanding the final
keyword is crucial for writing robust Java applications.
In this tutorial, we'll explore the different ways to use the final
keyword in Java, along with examples to illustrate its application.
1. final
with Variables
final
with VariablesWhen you declare a variable as final
, you cannot change its value once it has been initialized. This makes the variable a constant.
Syntax:
Example:
Explanation:
MAX_VALUE
is declared asfinal
, meaning its value cannot be changed once assigned.Trying to reassign a value to
MAX_VALUE
will result in a compilation error.
Output:
2. final
with Methods
final
with MethodsWhen a method is declared as final
, it cannot be overridden by subclasses. This is useful when you want to ensure that the method's behavior remains consistent across all subclasses.
Syntax:
Example:
Explanation:
The
show
method inParentClass
is marked asfinal
, preventing it from being overridden inChildClass
.Attempting to override the
show
method in the subclass will result in a compilation error.
Output:
3. final
with Classes
final
with ClassesWhen a class is declared as final
, it cannot be subclassed or inherited. This is useful when you want to prevent other classes from modifying the behavior of your class through inheritance.
Syntax:
Example:
Explanation:
FinalClass
is declared asfinal
, meaning no other class can extend it.Attempting to create a subclass of
FinalClass
will result in a compilation error.
Output:
4. final
with Reference Variables
final
with Reference VariablesWhen you declare a reference variable (e.g., an object or an array) as final
, you cannot reassign the reference to point to a different object. However, you can still modify the object's internal state (i.e., its fields or elements).
Example:
Explanation:
The
final
keyword prevents thenumbers
array reference from pointing to a new array, but the elements of the array can still be modified.Attempting to reassign the
numbers
reference will result in a compilation error.
Output:
5. final
and Constructors
final
and ConstructorsThe final
keyword cannot be applied to constructors. Constructors are meant to initialize objects, and since they cannot be inherited or overridden, there is no need to mark them as final
.
Example:
Explanation:
The
final
keyword is used to declare thevalue
variable, which is initialized in the constructor.Constructors cannot be marked as
final
.
Output:
6. Advantages of Using final
final
Security: By using
final
, you can prevent accidental changes to variables, methods, or classes, ensuring that your code behaves as expected.Performance: The JVM can optimize
final
variables and methods, leading to better performance.Design Clarity: Declaring a method or class as
final
clearly communicates your intent that it should not be overridden or extended.
7. Common Use Cases for final
final
Constants: Use
final
with variables to create constants that should not change throughout the program.Immutable Classes: Use
final
with classes to create immutable classes that cannot be subclassed.Preventing Method Overriding: Use
final
with methods to ensure that they cannot be overridden by subclasses, preserving the method’s original behavior.
Conclusion
The final
keyword in Java is a powerful tool for creating constants, securing methods from being overridden, and preventing classes from being subclassed. By understanding and using the final
keyword effectively, you can write more reliable and maintainable Java programs.
For more Java tutorials and resources, visit codeswithpankaj.com.
Last updated