Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, the final
keyword is a modifier that can be applied to classes, methods, and variables. In this tutorial, we will discuss the different uses and effects of the final
modifier in Java.
Example:
public class FinalVariableExample { // A final variable (constant) with an assigned value public static final double PI = 3.14159; // A final instance variable to be assigned in the constructor private final int id; public FinalVariableExample(int id) { this.id = id; } public int getId() { return id; } public static void main(String[] args) { FinalVariableExample example = new FinalVariableExample(42); System.out.println("ID: " + example.getId()); System.out.println("PI: " + PI); } }
In this example, the PI
variable is a final static constant, and the id
variable is a final instance variable assigned in the constructor.
Example:
public class FinalMethodExample { public static void main(String[] args) { BaseClass base = new BaseClass(); DerivedClass derived = new DerivedClass(); base.finalMethod(); derived.finalMethod(); } } class BaseClass { public final void finalMethod() { System.out.println("This is a final method in the base class."); } } class DerivedClass extends BaseClass { // Attempting to override the final method will result in a compilation error // @Override // public void finalMethod() { // System.out.println("Trying to override a final method."); // } }
In this example, the finalMethod
in the BaseClass
is declared as final, preventing it from being overridden in the DerivedClass
.
Example:
public final class FinalClassExample { private final int id; public FinalClassExample(int id) { this.id = id; } public int getId() { return id; } public static void main(String[] args) { FinalClassExample example = new FinalClassExample(42); System.out.println("ID: " + example.getId()); } } // Attempting to extend the final class will result in a compilation error // class DerivedClass extends FinalClassExample { // }
In this example, the FinalClassExample
class is declared as final, preventing it from being extended.
In conclusion, the final
keyword in Java is used to enforce immutability and prevent modification at different levels. It can be applied to variables, methods, and classes to create constants, prevent method overriding, and disallow class inheritance, respectively. Use the final modifier when you want to ensure the integrity of your design and protect against unintended changes.
Final keyword in Java and its significance
The final
keyword in Java is used to define entities that cannot be changed or extended.
final class FinalClass { // Class body } final int finalVariable = 42;
Using final for constants in Java
The final
keyword is often used for constants, ensuring their values cannot be modified.
public class Constants { public static final int MAX_VALUE = 100; }
Final fields and variables in Java
Fields and variables marked as final
cannot be reassigned once initialized.
class MyClass { final int finalField = 42; void myMethod() { final int finalVariable = 10; } }
Final methods and classes in Java
Declaring a method as final
prevents it from being overridden in subclasses. Similarly, a final
class cannot be extended.
class BaseClass { final void finalMethod() { // Method body } } final class FinalClass { // Class body }
Final vs. immutable in Java programming
While final
ensures that a reference cannot be changed, immutability ensures that the state of an object cannot be changed.
final List<String> immutableList = Collections.singletonList("immutable");
Final parameters in Java methods
Marking method parameters as final
indicates that their values cannot be changed within the method.
void process(final int parameter) { // Method body }
Final and inheritance in Java
When a class is marked as final
, it cannot be subclassed. However, methods within a final
class can still be inherited.
final class FinalClass { void finalMethod() { // Method body } } class SubClass extends FinalClass { // Valid, inherits finalMethod }