Current Android Release version:
Version Code: Pie
Version: 9
API level: 28

Understanding final keyword in Java

In Java, final keyword can be used with variable, method, class

final variable:
  • we can not change value of final variable once it is initialized
  • if final variable not initialized (blank final variable) at the time of declaration, it must be initialized in constructor
  • A static final variable that is not initialized during declaration can only be initialized in static block.
final method:
  • final methods can not be overridden but methods can be inherited
final class:
  • final class can not be inherited
Basic points to remember:
  • All variables declared in interface are by default final
  • constructor can not be declared final
  • If method parameters are declared final then the value of these parameters cannot be changed
  • Local final variable must be initialized during declaration

1 comment: