Compile Time Constants - II
Value of final variables are replaced at the time of compilation where they are used.
Only if the variable is a compile-time constant. Being final is necessary but not sufficient for a variable to be a constant. It must also be of primitive type or a String, and it must have an initializer which is a compile-time constant expression. If these conditions are not met, it's not a compile-time constant.
But Even though we can initialize final variable in the constructor.
You can set a final variable in a constructor, but this is not considered an initializer for the variable - so the final variable is not a compile-time constant.
So in this case they are maintained per Object and can be intitialized on the fly. So where these variables are managed in memory. In stack or heap?
Heap.
FinalVariableTest() {
b = 5;
}
final int b;
int c = b;
public static void main(String[] args) {
FinalVariableTest ft = new FinalVariableTest();
System.out.println("Hello World!");
}
}
You can set a final variable in a constructor, but this is not considered an initializer for the variable - so the final variable is not a compile-time constant.
If u compile this example it will result is compiler error because even though u initialize the final instance variable b with a value in the constructor still it is not considered an compile time constant
Initialize b with a constant like
final byte b = 5;
and assign
char c = b;
and comment b = 5 in the constructor( because once a final variable is assigned with a value u cannot re-assign(or)modify it)
now u will not get any compile time error. This is because now u are assigning a compile time constant to char.
I have only one doubt. Whether compile time constants available inside a method is stored in a Heap (or) Stack.
void display() {
final int value = 5;
}
An instance final variable must be assigned with some value before the constructor completes. Which means it has to be assigned a value atleast in the constructor.
An instance static final variable must be assigned with some value in the static block and a "instance static final variable" cannot be assigned a value in the constructor which will result in a compile time error.
If final variable is a class variable ("b") then initializing it in the constructor will result in compile time error.
static final byte b;
Because (JLS)
A field can be declared final (§4.5.4). Both class and instance variables (static and non-static fields) may be declared final.
It is a compile-time error if a blank final (§4.5.4) class variable is not definitely assigned (§16.7) by a static initializer (§8.7) of the class in which it is declared.
A blank final instance variable must be definitely assigned (§16.8) at the end of every constructor (§8.8) of the class in which it is declared; otherwise a compile-time error occurs.
class NumConvTest {
final int j; // 1
// final static int j; // 2
// int h = j; // 3
int t = call();
static int call() {
System.out.println("instance method call");
return 5;
}
static {
j =200;
}
NumConvTest() {
j = 2;
System.out.println("1000");
}
public static void main(String[] args) {
NumConvTest nc = new NumConvTest();
int b1 = nc.j;
System.out.println(b1);
}
}
O/P
instance method call
1000
2
Consider this program. This code works fine and prints 2 even when u initialize the final variable inside the constructor which is not a compile time constant. Because by the time nc.j is assigned to b1 the constructor would have completed its execution. But consider uncommenting "3". You will get compiler error. This is because before any statement in the constructor gets executed the instance variables in the class will be initialized. Hence j is not initialized to 2. So when you try to assign a final variable which has not been initialized to a constant to another instance variable then compiler will complain about it.
For the same case if j isn't a final variable then even if you assign j to another instance variable then you will not end up in compile time error because default value of instance variable which is 0 will be asigned to the another instance variable.
0 Comments:
Post a Comment
<< Home