This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
My understanding is that non-static fields are given default values before the object is instantiated, whether or not they are later given non-default initialization values when it is instantiated (Mughal/Rasmussen p. 342). Does this happen when the class is loaded, along with static members, or when the *new* operator is called on a class's constructor and before the constructor is actually run? Or . . . The problem I'm having is understanding why it is that, during initialization of an object, a field can be written to before it's declared, but can't be read before it's declared. If an int can have its default zero overwritten before the thread of execution reaches its declaration/initialization, why can't it be read? (I can't think of any reason why you'd ever want to, I just want to know how this works.) M/R provide this example (p. 333):
At the line labeled (3), how can there be a 'this'? The object is in the middle of being constructed. If it means *'this' so far*, or *this such as it is at (3)*, then why is this.width readable and width, as in line (2), not readable? There's something in this whole scenario I'm missing, that would probably answer all these questions. Thanks in advance. Dennis ps - this is my first visit to the ranch. This is probably one of Java's best kept secrets (not that it should be).
As Michael said, this is not really a beginner's question. The answer to your question is found in the Java Language Specification.
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).
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:
Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
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.
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.
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.) 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.
So you can see that the instance variables are created and initialized to their default values close to the beginning of the construction ... shortly after the class is defined (the this), but before the explicit initialization of the instance variables which is still before construction is completed. [ January 04, 2004: Message edited by: Marilyn de Queiroz ]
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Joined: Dec 31, 2003
Well that sure clears it up. I'll have to learn my way around the language specification pages.
Let's move this to the Intermediate forum.
Being a beginner I don't really know what's elementary and what's intermediate. I guess this means I'm being fined 10 posts at the beginner forum. (BTW - I actually have an old show saddle not far from my computer. Does this get brownie points to make up for part of the fine?) Thanks a lot for the link! Dennis