GeekInterview.com
Series: Subject: Topic:
Question: 30 of 124

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

Asked by: vishalanand_154 | Member Since Jul-2008 | Asked on: Jul 31st, 2008

View all questions by vishalanand_154

Editorial / Best Answer

Answered by: r.praveenkumar

View all answers by r.praveenkumar

Member Since Oct-2007 | Answered On : 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 - 6 of 6 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.

Yes  3 Users have rated as useful.
  
Login to rate this answer.
amreenkhader

Answered On : Jan 20th, 2012

View all answers by amreenkhader

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

  
Login to rate this answer.
jayy2kok

Answered On : May 31st, 2012

View all answers by jayy2kok

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.     }

  
Login to rate this answer.
Sam

Answered On : 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. }

  
Login to rate this answer.
Rohit Verma

Answered On : 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 )

  
Login to rate this answer.
Nitin Maulekhi

Answered On : Jul 18th, 2012

View all answers by Nitin Maulekhi

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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Question Categories

Core Java Interview Questions

EJB Interview Questions

Hibernate Interview Questions

Java Interview Questions

Java Patterns Interview Questions

JDBC Interview Questions

JMS Interview Questions

JSP Interview Questions

Java OOPS Interview Questions

Ruby Interview Questions

Servlets Interview Questions

Spring Interview Questions

Struts Interview Questions

Websphere Interview Questions

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.