final Java Keyword with Examples


As we know that inheritance enables us to reuse existing code, sometimes we do need to set limitations on extensibility for various reasons; the final keyword allows us to do exactly that.
In this short article, we’ll take a look at what the final keyword means for classes, methods, and variables - 
  1. The final keyword may be applied to a variable, indicating the value of the final variable can not be changed (It will be constant).
  2. The final keyword may be applied to a class, indicating the class may not be extended (subclassed).
  3. The final keyword may be applied to a method, indicating the method may not be overridden in any subclass
A final variable that has no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only.
Let's discuss usage of a final keyword with respect to variable, method, and class.

Java final variable

If you make any variable as final, you cannot change the value of a final variable(It will be constant).

Example of final variable

The below diagram demonstrates that the final variable name can't be reassigned which will result in compile time error.

Java final method

The final keyword may be applied to a method, indicating the method may not be overridden in any subclass.

When we design a class and feel that a method shouldn’t be overridden, we can make this method final. We can also find many final methods in Java core libraries.

Sometimes we don’t need to prohibit a class extension entirely, but only prevent overriding of some methods. A good example of this is the Thread class. It’s legal to extend it and thus create a custom thread class. But its isAlive() methods is final.

Java final method example

The below diagram demonstrates that the final method can't be overridden which will result in compile time error:

Java final class

The final keyword may be applied to a class, indicating the class may not be extended (subclassed).
If we look at the code of Java core libraries, we’ll find many final classes there. For example String, Wrapper classes and BigDecimal, BigInteger etc.

Java final class example

The below diagram demonstrates that the final class can't be extended which will result in compile time error.

What is blank or uninitialized final variable?

A final variable that is not initialized at the time of declaration is known an as a blank final variable.
If you want to create a variable that is initialized at the time of creating an object and once initialized may not be changed, it is useful. For example PAN CARD number of an employee.
It can be initialized only in the constructor.
Example of blank final variable
class Student{  
    int id;  
    String name;  
    final String PAN_CARD_NUMBER;  
...  
}  
Note that the final PAN_CARD_NUMBER variable not initialized. We can initialize a blank final variable only in a constructor.

Summary

  1. Variables marked as final can’t be reassigned.
  2. Methods marked as final cannot be overridden.
  3. Classes marked as final can’t be extended.

All Java Keywords 

  1. abstract Java Keyword
  2. assert Java Keyword
  3. boolean Java Keyword
  4. break Java Keyword
  5. byte Java Keyword
  6. case Java Keyword
  7. catch Java Keyword
  8. char Java Keyword
  9. class Java Keyword
  10. continue Java Keyword
  11. default Java Keyword
  12. do Java Keyword
  13. double Java Keyword
  14. else Java Keyword
  15. enum Java Keyword
  16. extends Java Keyword
  17. final Java Keyword
  18. finally Java Keyword
  19. float Java Keyword
  20. for Java Keyword
  21. if Java Keyword
  22. implements Java Keyword
  23. import Java Keyword
  24. instanceof Java Keyword
  25. int Java Keyword
  26. interface Java Keyword
  27. long Java Keyword
  28. native Java Keyword
  29. new Java Keyword
  30. package Java Keyword
  31. private Java Keyword
  32. protected Java Keyword
  33. public Java Keyword
  34. return Java Keyword
  35. short Java Keyword
  36. static Java Keyword
  37. strictfp Java Keyword
  38. super Java Keyword
  39. switch Java Keyword
  40. synchronized Java Keyword
  41. this Java Keyword
  42. throw Java Keyword
  43. throws Java Keyword
  44. transient Java Keyword
  45. try Java Keyword
  46. void Java Keyword
  47. volatile Java Keyword
  48. while Java Keyword

Comments