Use of final Keyword in Java

Category: Java   Tags: Java, Java Basic, Java final Keyword

In Java final keyword can be applied on field, class and methods.

final Variable

A final variable if just like a constant, you can not modify it once a value has been assigned.

FinalFieldExample.java

                          package com.tutorial.javabasic;

                          public class FinalFieldExample {

                              public static void main(String s[]) {
                                 final int a = 1000;
                                 a = 2000;

                                 final int b;
                                 b = 201;
                                 b = 300;
                              }
                          }
                        

Here, a has already been assigned 1000, so assigning again anything else to a will show compile time error: cannot assign a value to final variable a.

b is first declared and then assigned 201 therefore assigning again 300 will show compile time error: variable b might already have been assigned.

A final variable is used as constants e.g.

                          final int EXECUTION_SUCCESS = 0;
                          final int EXECUTION_FAILED = 1;
                          final int EXECUTION_PENDING = 2;
                        

final Method

A final method can not be override in its subclass. This allows the original developer to create functionality that cannot be changed by it subclasses.

FinalMethodExample.java

                          package com.tutorial.javabasic;

                          class A {

                              final void printText() {
                                  System.out.println("Print something.");
                              }
                          }

                          class B extends A {
                              /*
                                This will show compile time error: printText() in B cannot override printText() in A
                                overridden method is final
                              */
                              void printText() {
                                  System.out.println("Print something else.");
                              }
                          }

                          public class FinalMethodExample {

                              public static void main(String s[]) {
                                  B b = new B();
                                  b.printText();
                              }
                          }
                        

Here, B is extending class A and overriding A's method printText. printText inside A is declared as final therefore this code won't even compile as we are trying to override a final method. Removing the final keyword will solve the problem.

Let us see one example use case of final method:

                          public class Counter {

                              private int counter = 0;

                              public final int count() {
                                  return counter++;
                              }

                              public final int reset() {
                                  return (counter = 0);
                              }
                          }
                        

Now let say count method is not declared final. In that case, a subclass can modify the value of counter as below:

                          public class DummyClass extends COunter {
                            public int count() {
                                super.count();
                                super.count();
                                super.count();
                                super.count();
                                return super.count();
                            }
                          }
                        

Using final will stop this kind of misuses.

final Class

A final class is simply a class that can't be extended. But a final class can still extend another non-final class.

FinalClassExample.java

                          package com.tutorial.javabasic;

                          final class A {

                          }

                          /*
                          This will not compile. It will throw compile time error: cannot inherit from final A
                          */
                          class B extends A {

                          }

                          public class FinalClassExample {

                              public static void main(String s[]) {
                                  B b = new B();
                              }
                          }
                        

This Java program will not compile as we are trying to extending a final class.