Why is that the final variable has to be initialized as soon as it is declare?

Questions by vishalanand_154

Editorial / Best Answer

r.praveenkumar  

  • Member Since Oct-2007 | Aug 1st, 2008


when a variable is declared as final we cant change its value again. we use final variable to declare a constant so when we declare a variable as final and if we were allowed to not to give its value at the time of declaration it would not make sense so people who designed java made mandatory to give the value when we declare a final variable. then only it will be sensible.
final PI=3.14;
then we cant change PI's value. think if the above statement do not contain a vl it would not make sense.

Showing Answers 1 - 18 of 18 Answers

when a variable is declared as final we cant change its value again. we use final variable to declare a constant so when we declare a variable as final and if we were allowed to not to give its value at the time of declaration it would not make sense so people who designed java made mandatory to give the value when we declare a final variable. then only it will be sensible.
final PI=3.14;
then we cant change PI's value. think if the above statement do not contain a vl it would not make sense.

A variable can be declared but when program execution is running one can change its variable but when it reaches a final result the varable cannot be changed..hence it is declared as final variable

  Was this answer useful?  Yes

jayy2kok

  • May 31st, 2012
 

In java its not necessary to initialize the variable as soon it is declared,
You can just define the final variable, and initialize it later.

as below

Code
  1.     public static void main(String[] args) {

  2.        final String xyz;

  3.        xyz="bbc";

  4.        System.out.println(xyz);

  5.     }

  Was this answer useful?  Yes

Sam

  • Jul 6th, 2012
 

It is not necessary to initialize the final variable at the time of its declaration. We can initialize the final variable later also.This initialization can be done inside a constructor. The final variable declared in this manner is known as blank final variable

Code
  1. public class A

  2. {

  3.       final int x;

  4.       public A()

  5.       {

  6.             x=10;

  7.       }

  8. }

  Was this answer useful?  Yes

Rohit Verma

  • Jul 10th, 2012
 

Like any variable, a final variable can also be first declared and then assigned a value. But it can be only assigned a value once and only once ( as its final variable ).

for example :-
final String s1; // declared only - not assigned some value.
s1 = "test1"; // assigned value
s1 = "test2"; // not allowed. will not compile.

Similarly a final reference variable can only point to only object ( we can change the properties of object referred though )

  Was this answer useful?  Yes

final keyword can be applied on:-

1.class
2.member functions(only non-static)
3.data-members(both static and non-static)
4.local variables.

1.by making a class final ,that class cannot be inherited.
2.by making d function final,that function cannot be overriden by d child class.
3.by making local variable final,it will become final and we cannot change d value throughout d function.
4.for non-static data members,d value of data members for every object should be different,otherwise every variable would have d same value again and again.

so to avoid dis a new concept came i.e, Blank Final variable.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions