File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Instantiation/Forward referencing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instantiation/Forward referencing" Watch "Instantiation/Forward referencing" New topic

Instantiation/Forward referencing

Dennis Diment

Joined: Dec 31, 2003
Posts: 7
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.
ps - this is my first visit to the ranch. This is probably one of Java's best kept secrets (not that it should be).
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

Let's move this to the Intermediate forum. Don't wanna scare the beginners off.
[ added link -ds ]
[ January 05, 2004: Message edited by: Dirk Schreckmann ]

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
I agree. Here's the link:
subject: Instantiation/Forward referencing
It's not a secret anymore!