First, we will create a new class. If we declare any variable as final
, that means the value cannot be changed again. Let's consider the following code:
package coreJava; public class finaldemo { public static void main(String[] args) { //TODO Auto-generated method stub final int i=4; //constant variables } }
As you can see, we have declared the integer value as 4
. This means we cannot change this value to another number. If we try to do that, it throws an error saying Remove 'final' modifier of 'i'
. This keyword is useful if we want a value to be constant.
If we mark a class as final
, it will throw an error because when we change the access mode to final
, we are not able to use that as a parent class. In other words, we will not be able to inherit our properties from it. If we want to inherit our properties, we need to change it back to public
. The key logic for the final keyword is that, once written, we cannot override final
methods. So these...