Final means immutable. Final classes cannot be inherited or instantiated. Final methods cannot be overriden and final variables cannot be changed
Final Classes and use
Final classes is a perfect class where nothing needs to be modified , changed , anything need not be added or deleted from the class. for e.g String class in java is final. We cannot override any methods from string class or instantiate the class.
Final Variables and use
Final variable can be used to declare constants which cannot be modified ever. To declare constants like PI. Final variables are read only.
Final methods and use
Final methods cannot be overriden. If you think you have written perfect method or if u dont want to change the method functionality and it should remain same for all the subclasses method should be made Final
Advantages
1) Final methods, variables are faster as they are cached by JVM at compile time
2) In multithreaded environment they are safe and overhead is avoided w/o using synchronization.
Final Classes and use
Final classes is a perfect class where nothing needs to be modified , changed , anything need not be added or deleted from the class. for e.g String class in java is final. We cannot override any methods from string class or instantiate the class.
Final Variables and use
Final variable can be used to declare constants which cannot be modified ever. To declare constants like PI. Final variables are read only.
Final methods and use
Final methods cannot be overriden. If you think you have written perfect method or if u dont want to change the method functionality and it should remain same for all the subclasses method should be made Final
Advantages
1) Final methods, variables are faster as they are cached by JVM at compile time
2) In multithreaded environment they are safe and overhead is avoided w/o using synchronization.
No comments:
Post a Comment