final keyword in Java
The keyword "final" holds a reserved status, serving as a means to impose restrictions on users. It can be applied to member variables, methods, classes, and local variables. The usage of the final keyword signifies that a variable is intended to be initialized only once, thereby ensuring that subsequent modifications are disallowed by the compiler, enforcing a singular initialization.
The java final keyword can be used in various context:
- final variable - A variable declared as final prevents the content of that variable being modified
- final method - A method declared as final prevents the user from overriding that method
- final class - A class declared as final cannot be extended thus prevents inheritance
Once a variable is marked as final, its value cannot be changed once initialized.
Characteristics and behaviors of final variables
Here are the key characteristics and behaviors of final variables in Java:
- Constant Value: A final variable represents a constant value that remains unchanged throughout the program's execution.
- Initialization: A final variable must be initialized at the time of declaration or in the constructor of the class if it is an instance variable. If it is a static variable, it should be initialized in the static initializer block or at the time of declaration.
- Immutable Nature: Once a final variable is assigned a value, it cannot be reassigned or modified. Any attempt to assign a new value to a final variable will result in a compilation error.
- Method Parameters: Final can also be used to declare method parameters. It indicates that the parameter's value cannot be modified within the method.
- Instance and Static Context: Final variables can be declared as instance variables or static variables. Instance variables are associated with each instance of a class, while static variables are shared among all instances of the class.
- Thread Safety: Final variables contribute to thread safety. If a final variable is properly initialized and shared among multiple threads, its value remains consistent and cannot be modified concurrently.
- Performance Optimization: The use of final variables allows the compiler to perform optimizations. It can inline the values and eliminate unnecessary checks, resulting in potential performance improvements.
- Naming Convention: By convention, the names of final variables are written in uppercase letters with underscores separating words (e.g., MAX_SIZE, PI, DEFAULT_TIMEOUT).
When the final keyword is applied to a variable within a class in Java, it signifies that the variable can only be initialized once, and its reference cannot be modified once assigned. In the case of object references, final ensures that the reference remains constant, always pointing to the same object. However, it does not provide any assurances regarding the immutability of the values within the object being referred to.Example
The final keyword in Java is used to declare variables that cannot be modified once initialized, ensuring immutability and constant references, but it does not guarantee the immutability of the object's internal values.