siaooo
siaooo

Reputation: 1825

Declaring constant with no value

If I declare a constant like static final double PI; I'll get The blank final field PI may not have been initialized error. Why can't I declare it this way first and initialize it later?

UPDATE: I might not know the initial constant value and have to initialize it after some logic so that I can have a value for it. That's why I need to initialize it later.

Upvotes: 2

Views: 2145

Answers (9)

Sashi Kant
Sashi Kant

Reputation: 13465

Because you are trying to read the variable before it is set.

In JAVA you have the provision to set it, anywhere before it is read. But in your case, you have marked it as final and that too, static,which means

final : You can set the value only once
statis: You have made this variable static which means it will be created one per class.

Upvotes: 0

Vallabh Patade
Vallabh Patade

Reputation: 5110

Variable is declared as static means, when class is get loaded into memory, all static variables get loaded into the memory as well. On top of that that variable is final means it has to be some value right at the time of class loading. Initialising it in non static block of code means changing its value from nothing(null) to something newly assigned value.

You can see this with the example that even if you didn't initialise a static final variable, you can do it in static block of the class.

class Demo {
       static final String msgHeader;
       /*
           Some lines of code
       */
       static { 
                msgHeader="Please Verify the Input";
       }
 }

Upvotes: 2

MGPJ
MGPJ

Reputation: 1072

Like all other normal variables in java, the constant variable also should be declared before it is being used first inside any method. If you need to use that variable without initialization, then you should declare that variable as a class variable instead of declaring the variable into the method and this is a constant variable and if you declared this variable as a class variable then you cannot modify its value.

Upvotes: 0

salihgueler
salihgueler

Reputation: 3622

i guess you have to initialize a first value for that to solve this problem

static final double PI=3.14;

can solve your problem

Upvotes: 1

subramanya
subramanya

Reputation: 26

Since the field PI is marked as "final" you cannot change it later by assigning new value to the field. Final also marks the field PI as constant. So you need to specify the value during declaration itself which cannot be modified at all .

Upvotes: 0

Pradeep Simha
Pradeep Simha

Reputation: 18123

Because constants cannot change once it has been assigned. :)

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533580

You can initialise it later in the code. You just have to initialise it before it can be used.

It is not write once memory and reads won't block until a value is set.

Upvotes: 2

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70949

Java has to make sure that a final field is only initialized once and never changes. This can only happen during initialization.

Upvotes: 1

bellum
bellum

Reputation: 3710

Because than it will be not constant cause you can change it value in different places of your program.

Upvotes: 0

Related Questions