Wednesday, July 7, 2021

The Ultimate Guide of final modifier in Java

Hello Java Programmers, the final variable is a very important concept in Java. It's a modifier that you can apply on variables, methods, and classes and when you apply the final modifier it can make variables immutable, prevent the method from overriding in subclasses, means no polymorphism, and when you make a class final in Java it cannot be extended anymore, taking out Inheritance from the picture. There are both pros and cons of using the final modifier in Java and that's why it's very very important for a Java developer to have an in-depth knowledge of the final modifier in Java and that's what you will learn in this article. 

In the past, I have shared many examples of final variable and final methods in Java, and in this article, I will share important points and concepts about final modifiers which I believe every Java programmer, both beginner, and experienced developer should know. 

Here are some important details about the final modifier in Java:

1. Eligibility

The final is a modifier in Java, which can be applied to a variable, a method, or a class. Though, you can have a final block. Also, you can use the final modifier with local variables, class variables, as well as with instance variables. 

Similarly, you can use the final modifier with both static and non-static methods in Java, but you cannot apply the final modifier with abstract methods, that's not allowed. 

For classes, you can use the final modifier with both top-level and nested classes but you cannot make an abstract class final in Java. That's not allowed. 


2. final modifier with class

When a final modifier is used with a class then the class cannot be extended further. This is one way to protect your class from being subclassed and often sensitive classes are made final due to security reasons. This is also one of the reasons why String and wrapper classes are final in Java. 

At the same time, you cannot make an abstract class final in Java because once you make it abstract it means its incomplete, and the only way to use it by extending it but when you make a class final means you are saying that is completed and not be modified anymore, since both are mutually exclusive behavior, you cannot make an abstract class final in Java. 


final class Java example




3. final methods

When the final keyword is used with a method that cannot be overridden in Java, which means you cannot override the logic of the method in the subclass. This is also done to protect the original logic of the method. 

It's a compromise between making the whole class final or just making one method final. Since making a class final takes out the power of inheritance, sometimes it's better just to make the sensitive method final instead of the whole class.

One good example of the final method is the template method pattern, in which the template method which outlines the algorithm purposefully makes final so that no one can change the steps of the algorithm but at the same time it's allowed the client to define steps by extending the class and implementing the abstract method for individual steps. 

You can further see when to make a method final in Java to learn more about how to use the final keyword with methods in Java.


final methods Java example




4. final variables

When the final keyword is used with a variable then its value cannot be changed once assigned. Though this is a little bit tricky to understand especially when you make a reference variable final pointing to array or collection.

What this exactly means is that a final reference variable cannot point to another object but an internal state of the object can be changed like you can add or remove elements from the array or collection referenced by a final variable.

This is also one of the tricky questions from the Java interview and sometimes asked as to whether you can make an array or collection final in Java. Your answer should mention the fact that even though it is syntactically allowed, you can still modify the collection. 

There is a difference between making a reference variable pointing to collection final and creating a read-only or immutable collection.  You can further see these free Java courses to learn more about final variables and their usage. 

final variables Java example



When the static keyword is used with the final modifier then the variable becomes a compile-time constant. This means the value of the static final variable is copied to wherever it gets referred. 

This can create an issue if you don't understand the concept completely because if that variable is shared across multiple JARs then just updating its value in one JAR will not be sufficient, other JARs will still have old value.

It's very important to compile the whole project whenever you change the value of a public static final constant in Java. See how the public static final variable works in Java to learn more about this concept.

static final variables  example


A non-static final variable can also be a blank final variable if it's not initialized in the same line it has declared. Java allows a blank final variable but you must initialize that in all constructors. If you forget to initialize a blank final variable in all the constructors, the compiler will throw an error.

blank final variable example




7. Effective final variable

Along with lambda expression, streams, and a couple of other changes, Java 8 has introduced a new concept called effectively final variable, which allows a non-final variable to be accessed inside an inner class or lambda expression.  

Earlier, you cannot access a non-final local variable inside an inner or anonymous class but from Java 8 onwards you can provide its effectively final, I mean its value has not changed after assignment.

You can also join these free Java 8 courses and tutorials to learn more about the effectively final variable. The book explains all the important enhancements which matter in a crisp and clear way.

Effective final variable java




8. final class with methods

When you make a class final then all its methods effectively become final because a final class cannot be extended and without inheritance, you cannot override them. Some programmers argue that how about overriding final methods in the inner class, well that's not true because even an inner class cannot extend a final class. You can read more about this concept in good core Java courses.  It's one of the important concepts from the interview perspective.

The Ultimate Guide of Final Modifier in Java



9. local final variable

Last but not least, a final modifier can be used with both member variables and local variables declared inside a method or local code block. Earlier only final local variables can be accessed inside anonymous inner classes or local classes, but from Java 8 onwards even effectively final variable can be accessed as discussed above.

That's all about the final modifier in Java. It's truly special, as you can apply the final modifier with variables, methods, and classes and it provides a unique feature to them. Remember, if you make a reference variable final, you cannot assign it to a new reference. If you make a method final then you cannot override it and if you make a class final then you cannot extend it.

4 comments:

  1. Every point you mentioned are very crisp and clear. One can easily get what you want to deliver to them.

    ReplyDelete
    Replies
    1. Thank you Akshay and mulali, glad that you find the article useful.

      Delete

Feel free to comment, ask questions if you have any doubt.