The ‘final’ keyword in Java

Time to explore the ‘final‘ keyword.

When you use the keyword ‘final’ with a variable declaration, the value stored inside that variable cannot be changed come what may!!

So suppose you do this :

final int i =10;

After this, nowhere in your code can you change the value of ‘i’ i.e. ‘i’ will always have a value ’10′ .

So i = 11; //will give you a compile time error.

Now, suppose you did not initialize the final integer ‘i’ while declaring it.

final int i;

Can you assign any value to this variable now?

Yes, you definitely can. But only once!

So,

final int i;

i = 10; //This is fine

i = 11; //Compiler error

Now that was the core stuff about ‘final’ . Let’s learn a bit more in detail about it.

The ‘final’ keyword can be used for a class declaration or method declaration.

What happens when you use ‘final’ for a class declaration?

A: You cannot have a sub class of a class declared final i.e. No other class can extend this class.

What happens when you declare a method as final?

A: Simply put, the method cannot be overridden.

Can you override a method(Not declared as ‘final’) present inside a final class?

A: Obviously Not. Since there’s no way to extend a class declared final there’s no way of overriding its methods!

What happens when I declare a method inside a class as both ‘private’ and ‘final’ ?

A: Making a class both ‘private’ and ‘final’ makes the ‘final’ keyword redundant as a private method cannot be accessed in its sub class. But hey, you’ll be able to declare a method of the same name as in the base class if the method has been made private in the base class. But then that doesn’t mean you’re overriding the method. You’re simply declaring a new method in the sub class.

Note: You cannot make an ‘abstract’ class or method as ‘final’ because an ‘abstract’ class needs to be extended which will not be possible if you mark it as ‘final’!

I found this article on ‘final’ worth reading:

http://www.ibm.com/developerworks/java/library/j-jtp1029.html

Tags: , , , , , , ,

8 comments

  1. Variable which is marked final and not initialized can be initialized only once in constructor, not anywhere else.
    If the final variable is static, then it can be initialized only once in static initialization blocks.

  2. @lakshmi

    Yeah, very true :) Thanks for pointing that out. My article holds good for instance variables that are final.

    Class variables which are final are the ones that can be only initialized inside the constructor.

  3. string objects are immutable o.k .what is the reason to make a string object as immutable?
    plz clarify my doubt.

  4. @pravin In simple words, when you create 2 strings with same value ,

    String s = “Hi”;
    String t – “Hi”;

    Internally its just a single “Hi” that is present. Now if Strings were not immutable, then if you did a

    s.concat(“Bye”);

    This would change the value for variable “t” as well. Hope you got the point.

    It is for the very same reason why a simple (s==t) would return true in the case of String but not for other objects.

  5. Thanks for posting this valueable article.

  6. Jan-Hendrik van Heusden

    This explanation is clear and correct, yet I miss a really important point, which me be very naughty to beginners.
    That’s the use of final for variables (either local or instance variables) that point to Objects.

    // Declare final
    final MyClass myFinalClassVar = new MyClass();
    // other
    MyClass myOtherClassVar = new MyClass();

    // Can not assign an other value…
    myFinalClassVar = myOtherClassVar; // illegal

    // But I *can* change properties of the underlying object:
    myFinalClassVar.changeSomethingOfIt(newValue);
    // OK, so the variable can not reassigned, but the underlying objects’s property are still mutable!

  7. what do u mean by final keyword?
    what is the use of vector class?
    plz clearify it

Leave a comment