Originally posted by shadow liu:
As I remember, the calling sequence is below:
1) if there are super or this call in constructor, do it.
2) call superclass (till Object) constructor
3) then the initial part the class
4) the rest of the class constructor.
so this(a) is exec in 1)
but 'int a' initial will be exec in 3).
HTH.
12.5 Creation of New Class Instances
.
.
.
.
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 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. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type.)
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. In the example:
"JavaRanch, where the deer and the Certified play" - David O'Meara
Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden (��8.3). If there is not sufficient space available to allocate memory for the object, then creation of the class instance completes abruptly with an OutOfMemoryError. Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (��4.5.5).
An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.
For example, if the first constructor of ColoredPoint in the example above were changed to:
ColoredPoint(int x, int y) {
this(x, y, color);
}
then a compile-time error would occur, because an instance variable cannot be used within a superclass constructor invocation.
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
originally posted by Jane
When you use an explicit call ie this(a), another ctor for the same class is invoked after the parameter 'a' is evaluated. At this point, neither the class nor it's superclass has been created and no instance variables exist.
Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden (��8.3). If there is not sufficient space available to allocate memory for the object, then creation of the class instance completes abruptly with an OutOfMemoryError. Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (��4.5.5).
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Originally posted by James Du:
[B]
Why this(a) be rejected by compiler?
I think "a" has been initialized here and has a default value 0 and is legal for use.
Could anyone tell where in the JLS can i find the related rules against such reference.
Thanks in advance
James[/B]
originally posted by Madhu
I am a little confused here. "a" will be initialized and has a default value of zero ONLY after the class's constructor call has been completed. Right? Please explain
Consider Paul's rocket mass heater. |