Final Keyword in Java

Price: INR 8,999.00
Price: INR 15,999.00
Was: INR 16,999.00

Final keyword in Java is used to apply the restriction on the user.  In Java, you can declare the variable, method, and class with final keyword.

Final keyword is very useful in Java, Final keyword in Java has three different uses: create constants, prevent inheritance and prevent methods from being overridden.

 

Final Variable

Final is used to declare the local, class or Instance Variable constant. So we use the final to create constantly. You cannot change the value of a final variable once it is initialized.

 

Once a final variable has been initialized, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.

Like Arrays are objects so if a final variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array.

In the below-mentioned example, we are trying to change the final variable value. Let’s see how compiler will slap us-

Output:

Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The final field ConstantVariable.value cannot be assigned

at com.Seleniumbix.Corejava.OOPs.ConstantVariable.setVariable

 

Video Tutorial of Automation Testing:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

 

Blank Final Variable and It’s Uses

A final variable which is not initialized at the declaration time is known as Blank Final Variable. Blank final Variable must be initialized in the constructor of the class otherwise compiler will throw an error.

Now the question is what is the use of a final variable. Let’s understand this with a simple example. Let’s say we have an application for visitors in any organization. So suppose we have a class Visitors which have a field visitorNumber, So this field can be changed once new visitor will come. So we can declare visitorNumber is final we can’t initialize it at the time of declaration otherwise, all visitor will get the same visitor Number, which is not correct behavior of the application.

In such situation, we can declare it Blank final and initialize it later in the class.

Output:

Visitor Number is 1265

 

Final Keyword in Java:

A Java method with a final keyword is called a final method and it can not be overridden in sub-class. You should make a method final in Java if you think it’s complete and its behavior should remain constant in sub-classes. Final methods are faster than non-final methods because they are not required to be resolved during run-time and they are bonded at compile time.

If you will try to override the final method in the subclass then the compiler will throw an error.

Output:

Exception in thread “main” java.lang.VerifyError: class com.Seleniumbix.Corejava.OOPs.ClassB overrides final method printValue

 

Final Class:

If you declare the class as final then it can’t be inherited. A final class cannot be inherited, therefore, it will be a compile-time error if the name of a final class appears in the extends clause of another class declaration; this implies that a final class cannot have any subclasses.

 

Final & Immutable Class:

Final keyword helps to write an immutable class. Immutable classes are the one which can not be modified once it gets created. In Java String is the primary example of an immutable and final class (we will discuss this in String Tutorials in brief ).

Immutable classes offer several benefits one of them is that they are effectively read-only and can be safely shared in between multiple threads without any synchronization overhead. You can not make a class immutable without making it final and hence final keyword is required to make a class immutable in Java.

 

Facts about Final Keyword :

Remember these facts while using final in your application.

  1. You can declare Variables, Methods, and Classes as final in Java.
  2. The final member variable is constants and they must be initialized at the time of declaration or inside the constructor.
  3. The final variable value can’t be changed once initialized.
  4. A final method can’t be overridden.
  5. A final class can’t be inherited.
  6. Final, finally and finalize() they all are different concepts.
  7. All variable declared inside Java interface is implicitly final.
  8. Final and abstract are two opposite keyword and a final class can not be abstract in Java.

 

0 Comment

Leave a Reply