Since the JLS doesn't explicitly specify that behaviour, it might change in future releases or versions.A few minutes ago, I wrote:. . . The following example, however, shows it being added to the constructor . . .
Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:
1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
2. If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.
5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.
Thank you; I had forgotten about that section.Mike Simmons wrote:The JLS is quite specific about when each of these parts run. . . .
Constructors aren't methods, but they are replaced by the method “<init>” in the bytecode. As you say, copying the initialisers into “<init>” is how it is implemented, and implementation details are allowed to change. And you should be able to do all your programming without knowing anything about such implementation details.that's really shorthand for the method whose name is really "<init>" . . . .
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
Tim Holloway wrote:This method cannot be explicitly invoked (it doesn't have a syntactically legal name, for one thing)
Tim Holloway wrote:however, it's invoked after any superclass construction (if any)
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|