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:

  1. final variable - A variable declared as final prevents the content of that variable being modified
  2. final method - A method declared as final prevents the user from overriding that method
  3. 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:

  1. Constant Value: A final variable represents a constant value that remains unchanged throughout the program's execution.
  2. 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.
  3. 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.
  4. Method Parameters: Final can also be used to declare method parameters. It indicates that the parameter's value cannot be modified within the method.
  5. 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.
  6. 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.
  7. 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.
  8. 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
final class TestClass{ //this class can't be subclassed/extended final double pie=3.14;//this variable can only be initialized once private final Integer testMethod(final String input){ //this method can't be overidden return 0; } }

Conclusion

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.