Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instantiation/Forward referencing

 
Dennis Diment
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic