Implement Final keyword Practically in Java

Hi friends, in the last post we discussed about important points of final keyword. Now we are going to practically prove all the points of the final keywords. If I leave any point then please inform me because final keywords are also very important, concept should be clear.

Check out this simple program code: -

class fin1
{
public static void main(String[] ar)
{
final int a=100;
}
}

If you compile this program, it will show no error.

Now check out this program and try to notice the main difference: -

class fin1
{
public static void main(String[] ar)
{
final int a=100;
a=200;
}
}

Now if you compile this program, it will show 1 error. What can be the error. We have already declared variable a as final and have assigned a value = 100. Since we have assigned final variable a = 100 and according to the statement of the final keyword

If we declare any variable as final, it will be constant.

So a = 100 is already constant, it can not be changed but we tried to change by reassigning a new value a = 200. So we got an error message.Check out the screen shot of the compilation below

This programming of the final keyword in Java proofs the statement : -

If we declare any variable as final, it will be constant.

So now you know how to practically proof it.


Share this Post[?]
        

FINAL keyword in Java Language

Last time we discussed about the this keyword. Today we will discussed about another keyword called final. So note down the important points of the final keyword: -

  1. Final is a keyword which can be used with variables, methods and classes.
  2. If we declare any variable as final, it will be constant.
  3. If we declare any method as final, it cannot be overridden.
  4. If we declare any class as final, it can’t be inheritance.
  5. Final is the only modifier which can be used with local variables.
  6. Delay initialization of final variable are possible only within constructor.
  7. Final variables will never get default variables.


Share this Post[?]
        

« Previous PageNext Page »