Tony Morris
Java Q&A (FAQ, Trivia)
note that UN is known at compile time
and un() is not (it is method !)
so during memory allocation i is initialized with UN=1
and j is initialized with default =0
why i is initialized before constructor A()
Mike Gershman
SCJP 1.4, SCWCD in process
Originally posted by Mike Gershman:
A class's initializers are executed before its constructors (not counting the first lines of the constructors).
Mike Gershman
SCJP 1.4, SCWCD in process
Originally posted by Evgeni Shuster:
to Alton
According to the chapter 16 in JLS:
blank final field must have definitly assigned value when any access of it's value occurs
in modified version we access value of i ( in constructor A() ) before we assign value to it ( in constructor B() )
I still think, the reason for wrong behaviour is somewhere in implementation of JVM (some kind of optimization or just bug
[ February 25, 2005: Message edited by: Evgeni Shuster ]
Each local variable (�14.4) and every blank final (�4.5.4) field (�8.3.1.2) must have a definitely assigned value when any access of its value occurs. A Java compiler must carry out a specific conservative flow analysis to make sure that, for every access of a local variable or blank final field f, f is definitely assigned before the access; otherwise a compile-time error must occur.
Tony Morris
Java Q&A (FAQ, Trivia)
You must also differentiate between a default initial value from a proper initial value. A blank final variable will be initialized to a default initial value during the initialization phase. But it needs a proper initial value before its used because it is deemed as constant
here I access i in B before assigning a value to it,
and this looks even more forbidden than accessing it in A
Originally posted by Evgeni Shuster:
I perfectly understand what Alton says.
I just say that it should not work this way.
In first example instance variable initializer executed before super constructor (must be after, according to JLS).
In second and third examples final field accessed before a value is assigned to it. (final field must have PROPER value before first access to it)
It is impossible to do thease things without using super constructor.
What a show! What atmosphere! What fun! What a tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|