Today I came across strange behavior that I could not understand why.
Imagine we have a final variable in a typical Java class. We can initialize it immediately or in the constructor of the class as follows:
public class MyClass {
private final int foo;
public MyClass() {
foo = 0;
}
}
But I do not know why we cannot call the method in the constructor and initialize fooin this method, for example:
public class MyClass {
private final int foo;
public MyClass() {
bar();
}
void bar(){
foo = 0;
}
}
Because I think that we are still in the constructor stream, and it is not finished yet. Any hints would be much appreciated.
source
share