![]() ![]() Such a rule would closely follow the familiar existing rules for blank final fields. This would allow constructors to, for example, do housekeeping prior to superclass construction. It does so, however, in a heavy-handed way, by taking what is really a semantic requirement ("initialize the superclass before accessing the new instance") and enforcing it with a syntactic requirement (" super() or this() must be the first statement").Ī rule that more carefully addresses the requirement to ensure top-down initialization would allow arbitrary statements prior to superclass construction as long as the instance's fields are not read until superclass construction completes. This does indeed guarantee top-down initialization. ![]() To enforce top-down initialization, the JLS requires that invocations of this() or super() in a constructor always appear as the first statement. For example, it is common for a constructor to invoke superclass methods to configure or prepare the object for a specific task. This guarantee is important because constructors often need to rely on functionality in a superclass, and the superclass would not be able to guarantee correct behavior without the assumption that its own initialization is complete. This ensures that, at each level, a constructor can assume that the fields in all of its superclasses have been initialized. Thus objects are always initialized from the top down. To keep this process orderly, the JLS requires that superclass constructors execute prior to subclass constructors. An object is not fully initialized until after every class constructor in its hierarchy has initialized its own fields and returned normally. Each group of fields is initialized in a separate step by a corresponding constructor in those fields' defining class. An object's state is the composition of groups of fields: the group of fields defined in the class itself plus the groups of fields defined in its superclasses. For example, dataflow analysis ensures that every final field is assigned a definite value.įor classes in a non-trivial class hierarchy, object initialization does not occur in a single step. In order to ensure orderly object initialization, the JLS includes a variety of rules specifically related to object construction. The introduction of new language concepts - The rules for code appearing before this() or super() should be familiar and intuitive to developers, similar to the rules for code appearing inside this() and super() parameter lists, which also executes before superclass construction.īefore an object can be used, its state must be initialized. ![]() For example, the JVMS allows a constructor to write to the same final field multiple times, whereas the JLS does not. In any case, the JLS and JVMS address different problem domains, and therefore it is reasonable for them to differ in what they allow. Maximizing Java Language Specification (JLS) and JVMS alignment - These changes will bring the JLS and JVMS into closer alignment, but whether or how to harmonize them completely is beyond the scope of this work. Modifications to the Java Virtual Machine Specification (JVMS) - This JEP may prompt reconsideration of the JVMS's current restrictions on constructors, but we do not intend here to revise the JVMS. ![]() Preserve existing safety and initialization guarantees for constructors.ĭo not change the behavior of any existing program. In constructors, allow statements that do not access the instance being created to appear prior to invocations of this() or super(), allowing a more natural placement of initialization logic that currently must be shoe-horned into static methods, super() parameter expressions, or else is simply impossible to express.Ĭorrect an error in the specification which defines constructor invocations as a static context. In constructors in the Java programming language, allow statements that do not reference the instance being created to appear before this() or super(). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |