Today I'm facing a strange behavior which I couldn't figure out why.
Imagine we have a final variable in a typical class in Java. We can initialize it instantly or in class constructor like this:
public class MyClass {
private final int foo;
public MyClass() {
foo = 0;
}
}
But I don't know why we can't call a method in constructor and initialize foo
in that method, like this:
public class MyClass {
private final int foo;
public MyClass() {
bar();
}
void bar(){
foo = 0;
}
}
Because I think we are still in constructor flow and it doesn't finished yet. Any hint will be warmly appreciated.
A blank final variable can be initialized inside an instance-initializer block or inside the constructor. If you have more than one constructor in your class then it must be initialized in all of them, otherwise, a compile-time error will be thrown.
A final variable can be initialized only once. A final variable at class level must be initialized before the end of the constructor.
Declaring final variable without initialization If you declare a final variable later on you cannot modify or, assign values to it. Moreover, like instance variables, final variables will not be initialized with default values. Therefore, it is mandatory to initialize final variables once you declare them.
A copy constructor is a member function that initializes an object using another object of the same class.
First, assigning the value at declaration time is copied into every constructor for you by the compiler. Second, you can use a method to initialize the value, but you need to return
it for that to work. As others' note, you are required to ensure this value is set once.
public class MyClass {
private final int foo = bar();
private static int bar() {
return 0;
}
}
Which is equivalent to
public class MyClass {
private final int foo;
public MyClass() {
this.foo = bar();
}
private static int bar() {
return 0;
}
}
Note that bar
is static
, because otherwise you need an instance to call it.
You can only initialize a final variable once. There are three forms of final variables:
For class final variables, the variables can be initialized in either the declaration or static initializer:
class Program {
static final int i1 = 10;
static final int i2;
static {
i2 = 10;
}
}
For instance final variables, the variables can be initialized in the declaration, instance initializer, or constructor:
class Program {
final int i1 = 10;
final int i2;
final int i3;
{
i2 = 10;
}
Program() {
i3 = 10;
}
}
For local final variables, the variables can be initlialized in the declaration or any place after its declaration. The local final variables must be initialized before they are used.
class Program {
void method() {
final int i1 = 10;
final int i2;
System.out.println(i1);
i2 = 10;
System.out.println(i2);
return ;
}
}
Source: Refer link Reference Link
Final modifier on field (or variable) means that compiler will ensure that both of the following are true:
For your code, none of those is guaranteed:
It might be tempting to use private method rather than package-private. While it could guarantee you both of those conditions (unless you try to break it by reflection), javac still will not accept it, because it is not so powerful. There are some good reasons:
Instead of such mouse-cat-hunt, the language designers have decided to support just some well understood cases. In other cases, the code can be probably refactored. So, language creators can focus on some more important aspects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With