Forums Register Login

Instantiation/Forward referencing

+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
Let's move this to the Intermediate forum. Don't wanna scare the beginners off.
+Pie Number of slices to send: Send
Welcome to JavaRanch, Dennis.

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.



12.5 Creation of New Class Instances JLS 12.5

(Bolding is mine)

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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
We'll be keeping an eye on your aberrant categorizing, Dennis. Step out of line again and Marilyn won't use boldface in her next citation.
+Pie Number of slices to send: Send
Men call me Jim. Women look past me to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1178 times.
Similar Threads
Instantiation/Forward referencing
Forward reference
Forward Referencing, Initializers
question regarding initialzer.
forward reference doubt
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:20:05.