Difference between final & finally & finalize keyword?
The keyword final is an access modifier, finally is a block and finalize is a method. The keyword final is applicable to the classes, variables and methods of the classes finally is a block associated with the try catch block that is used to handle exceptions, finalize is a method that operates only on objects. The variable once declared as final becomes constant and canâ€™t be reassigned again, a method declared as final canâ€™t be overridden, and class once declared as final can never be inherited. The finally block is used to clean up the resources utilised by try and catch block. The finalize method is used to clean up the resources used by an object before the object is destroyed.
- Final is a modifier applicable for classes, methods, and variables.
- If a class declared as final then we can extend that class i.e. We can create a child class for that class i.e. Inheritance is not possible for final classes.
- If a method is final then we can’t override that method in the child class.
- If a variable declares as final then we can’t perform re-assignment for that variable.
- If you make a classÂ finalÂ means it cannot be subclassed. Making a class final automatically makes all its method final and this is sometimes required due to security reason, This is one of the reasonsÂ Why String is final in Java. In short, final is not related at all with eitherÂ finallyÂ orÂ finalizeÂ keyword. final keyword also helps to write Immutable classes which are critical for designing thread-safe multi-threading system and reducing the amount of synchronization.