Tutorials

Java Keywords


Java final Keyword


final keyword in java has huge impact on the variable, method or class.

In this chapter we will look at the impact that it creates on each of them one by one with proper example.

 

final keyword is used to impose certain restrictions

How final can be used in java?

  • Final can be used with variables to give it a permanent value (declare constant) to be utilized by others who wants to use this variable in any way and anywhere. 
  • Final can be used with class to prevent it from being inherited by any of its sub-class. 
  • Final can be used with methods to prevent it from being overridden.

final with variables

final variables have the following properties:

  • final variables cannot be reinitialized once assigned a value.
  • final reference variables cannot refer to a different object once the object has been assigned to the final variable.
  • final reference variables must be initialized before the constructor completes.

Example


public class FinalVariable {

 public static void main(String[] args) {

  final double pi = 3.14159;
  pi = 3.14;   //comile-time error
  ...................................

  ...................................

  ...................................


     }

}

OUTPUT:

comile-time error saying : The final local variable pi cannot be assigned. It must be blank and not using a compound assignment

 

Check your understanding on final variables 

final int z = 4;

int y = z++;

 

What is the fate of this code?

Would it compile?

 

No is the simple answer.

Be careful with this kind of code that use the increment/decrement operators on

a final variable. As you are aware by now that final variables can't be changed, the increment and decrement operators can't be used with them, and any attempt to do so will result in a compiler error

final with class in java

When you apply final at class level then you put restriction on the class inheritance.

 

A final class cannot be inherited by any other class.

 Let's see the example given below.

 

Source: FinalClass.java


public final class FinalClass {

        public void simpleInterest() {

        int p = 1000;

        int r = 5;

        int t = 2;



        int si = (p*r*t)/100;

        System.out.println(si);

        }

}

Now interest.java tries to extend FinalClass


public class interest extends FinalClass {         // compile-time error
}

OUTPUT:

compile-time error : The type interest cannot subclass the final class FinalClass

 

 

final with method in java

When you put final at method level then you restrict it from being overriden in it sub-classes.

 

final method cannot be overriden in the sub-class

Source: SI.java

 

public class SI {

        final void simpleInterest() {

        int p = 1000;
        int r = 5;
        int t = 2;

        int si = (p*r*t)/100;
        System.out.println(si);

        }

}

Now interest.java tries to override simpleInterest() of FinalClass

 

public class interest extends SI {         

        void simpleInterest() {    //compile-time error

        }

}

OUTPUT:

compile-time error : The type interest cannot override the final simpleInterest()

 

So the conclusion is compile time error will follow if you try to override final method.
 



Please Share this page
Views : 87
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!