which variable is used the assign the initial and final value
Answers
Answered by
2
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.
For example, following program works fine.
class Test {
final int i; // i could be assigned a value here also
Tets() {
i = 10;
}
//other stuff in the class
}
If we make i as static final then we must assign value to i with the delcaration.
class Test {
static final int i = 10; // Since i is static final, it must be assigned value here only.
//other stuff in the class
}
For example, following program works fine.
class Test {
final int i; // i could be assigned a value here also
Tets() {
i = 10;
}
//other stuff in the class
}
If we make i as static final then we must assign value to i with the delcaration.
class Test {
static final int i = 10; // Since i is static final, it must be assigned value here only.
//other stuff in the class
}
Similar questions