Originally Posted by
copeg
The first call in a child class is to call the parent class constructor, initialization of non-final variables sets their values to default, and initialization to custom values occurs inherently within the constructor (see
Initializing Fields (The Java™ Tutorials > Learning the Java Language > Classes and Objects) ). In affect, although not explicit this results in the following for a non-final variable (this might help you work out the workflow)
Adding final for initialization results in initialization of the variable prior to the constructor to a value different than the default value for the variable type. See the following article for a more thorough description
Object initialization in Java - JavaWorld
Thank you copeg,
By removing the
final from the line "private final int num = 10;", the program tends to have "making forward references" problem.
Child constructor -> Parent constructor -> initialization of parent's non-final variables, then goes into Parent's constructor body. I can understand these by reading the documents you mentioned.
The part "Calling subclassed methods from constructors" also explains a little bit of the case that I am having.
But, what about the code flow "walking"?
"final" variables initialization goes prior to the constructor, I couldn't find it from the "Object initialization in Java" link above.
Assume this case is true: the line "private final int num = 10;" is going to be executed TWICE ?
I am traveling with the code line by line under debug mode, and when the line "private int x = 4;" is held, Parent.value is already assigned to be 10. Afterward, the debugger will just step into the next line "private final int num = 10;" and step out with no error; final "int type" variable being assigned a value twice?
? x 1000, Please Help......