Final keyword in Java

Shubham Arya
2 min readDec 4, 2020

--

Final keyword in Java can be used in multiple places and I have seen whole chapters dedicated to this particular topic.

But, I want to share a very brief but to-the-point understanding of the final keyword in this article.

If you are using final keyword for any member(local variable/instance variable/method) it means there is no way that you can modify the value of that particular variable(if method, then you can’t override that method) throughout your program.

Compiler throws an error if you try to update a final variable
Compiler throws an error if you try to update a final variable

If you are declaring a class as final, then, it means that no other class(in same or different package) can extend that class(final class), in other words the final class can never be subclassed, but, the final class can be used as the “Superclass reference”.

Superclass reference just means that it can be used polymorphycially

Third is the case which you are referring to. If any object reference variable is declared as final, then it means that the final reference variable can never ever in its entire life refer to a different object but the data within the object(the object your reference variable is referring to) can be modified.

Hope it clears some of the concepts.

--

--

Shubham Arya
Shubham Arya

Written by Shubham Arya

Enthusiastic and fun loving. I am interested in technology and everything in & around it.

No responses yet