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[?]




