Sunday 26 June 2016

final keyword in java

Keyword 'final' says all about itself.
If you declare anything as final in java that means you can never modify it in future. Never.

Let's look at the ways in which final keyword is utilized in java.
  • Final can be used with variables to give it a permanent value (declare constant) to be utilized by others who wants to use this variable in any way and anywhere. 
  • Suppose you are creating a maths question on circle, and you want your students to use the value of pi(π) as 3.14159 in all the formula's that make use of pi. So what you can do is declare this pi as final variable and assign it the value and none of your student would be able to change this value during exam. (Now what if I declare a final variable but don't assign it a value?)
  • Final can be used with class to prevent it from being inherited by any of its sub-class. 
  • Final can be used with methods to prevent it from being overridden.

Note : Out of curiosity if you want to try and change the value of final variable or inherit a final class or override a final method then I am with you. Lets go ahead and see what we get :) 

Lets try to change final variable

final variables have the following properties:

  • final variables cannot be reinitialized once assigned a value.
  • final reference variables cannot refer to a different object once the object has been assigned to the final variable.
  • final reference variables must be initialized before the constructor completes.

public class FinalVariable {

public static void main(String[] args) {
final double pi = 3.14159;
pi = 3.14;        // You will get comile-time error saying : The final local                                                                             variable pi cannot be assigned. It must be blank and not                                                                           using a compound assignment



Check you understanding :

final int z = 4;
int y = z++;
What is the fate of this code? Would it compile?
No is the simple answer.

Be careful with this kind of code that use the increment/decrement operators on
a final variable. As you are aware by now that final variables can't be changed, the increment and decrement operators can't be used with them, and any attempt to do so will result in a compiler

Don't forget : final variable value cannot be changed and they are used to declare CONSTANT.  

Lets try to inherit final class

public final class FinalClass {
        public void simpleInterest() {
        int p = 1000;
        int r = 5;
        int t = 2;

        int si = (p*r*t)/100;

Now tries to extend FinalClass

public class interest extends FinalClass {         // compile-time error The type interest cannot                                                                                                     subclass the final class FinalClass

Written this much. Wait!
Can you see red line on the FinalClass(we have back-grounded it red here) in your Eclipse IDE which you are trying to inherit(extend). Put the cursor on this word to see the compile-time error shown above.

So the conclusion is compile time error will follow if you try to extend a class that is declared final.

Lets try to override final method

public class FinalClass {
        final void simpleInterest() {
        int p = 1000;
        int r = 5;
        int t = 2;

        int si = (p*r*t)/100;

Now tries to override simpleInterest() of FinalClass

public class interest extends FinalClass {         
        void simpleInterest() {         // compile-time error The type interest cannot                                                                                                     override the final simpleInterest()

So the conclusion is compile time error will follow if you try to override final method.

Q) Why would you ever mark a class final, if you can't change value of final variable, inherit final class or override final method?

You should make a final class only if you need an absolute guarantee that none of the methods in that class will ever be overridden. If you're deeply dependent on the implementations of certain methods, then using final gives you the security that nobody can change the implementation out from under you.

You'll notice many classes in the Java core libraries are final. For example, the String class cannot be subclassed. Use final for safety, but only when you're certain that your final class has indeed said all that ever needs to be said in its methods. Marking a class final means, in essence, your class can't ever be improved upon, or even specialized, by another programmer.
Suggestion : So unless you have a serious safety or security issue, assume that some day another programmer will need to extend your class.

You may also want to know about:

Thursday 2 June 2016

Benefit of having non final class

Imagine you find a problem with a method in a class you're using, but you don't have the source code.

So you can't modify the source to improve the method, but you can extend the class and override the method in your new subclass, and substitute the subclass everywhere the original super class is expected.

If the class is final, though, then you're stuck.

If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.