Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't we set the value of static final variable in static block through class name [duplicate]

Tags:

For example, consider code snap below:

public static final int a; public static final int b;  static {     a = 8;       // it's working     Test.b = 10; // compilation error Test.b cannot be assigned.  } 

Why can't we use Test.b = 10; inside a static block of the Test class itself? Without the class name it's working fine.

Is there any reason behind this?

like image 500
Snehal Patel Avatar asked Dec 16 '14 13:12

Snehal Patel


People also ask

Can we change the value of static final variable?

Finally, with the static final variable, it's both the same for each class and it can't be changed after it's initialized.

Can we initialize final variable in static block?

Yes of course: static final variables can be initialized in a static block but.... you have implicit GOTOs in that example ( try/catch is essentially a 'GOTO catch if something bad happens'). If an exception is thrown your final variables will not be initialized.

Can we assign value to static variable?

In Java, non-static final variables can be assigned a value either in constructor or with the declaration. But, static final variables cannot be assigned value in constructor; they must be assigned a value with their declaration.

Can we declare variables in static block?

Variables declared inside a block are accessible only inside of that block. Static or no. Variables declared inside a static method are static. They can only access other static variables or global variables.


1 Answers

A static final variable must be initialized before use. It may be initialized either directly at declaration time, or in a static block.

But when you use class.var = x it is not seen as an initialization but as an assignation.

With a JDK 7, the error is cannot assign a value to final variable.

That explains why it works if you remove the final keyword

class Test {      static final int a = 2; // initialization at declaration time     static final int b;     static final int c;      static {         b = 4;  // initialization in static block         Test.c = 6; // error : cannot assign a value to final variable c     }     ...  } 

EDIT

In the JLS the correct word for initialization is definite assignement

Extract from the JLS :

For every access of a local variable or blank final field x, x must be definitely assigned before the access, or a compile-time error occurs.

Similarly, every blank final variable must be assigned at most once; it must be definitely unassigned when an assignment to it occurs.

Such an assignment is defined to occur if and only if either the simple name of the variable (or, for a field, its simple name qualified by this) occurs on the left hand side of an assignment operator.

For every assignment to a blank final variable, the variable must be definitely unassigned before the assignment, or a compile-time error occurs.

emphasize mine, but I think this is the real reason for the error.

like image 67
Serge Ballesta Avatar answered Sep 19 '22 15:09

Serge Ballesta