Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Forward refs - revisited

 
Neil Mac
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Mughal Java cert book I'm currently studying states that
"An instance initializer cannot make forward rererences to member varibles declared after the initializer"
However, the following code compiles and runs fine....
public class Driver{
{
att2 = false;
}
private boolean att2 = true;
...
...

The only difference I can see is that the book refers to Java 1.2 but the compiler I'm using is Java 1.4. Is this something that has changed ? Or am I missing something ludicrously simple here
Thanks.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spider Man:
As we've said before, Welcome to JavaRanch!
We don't have many rules here at JavaRanch, but we do have a few. One of them is our Naming Policy.
We do appreciate your participation at our site, but we will enforce our naming policy; please change your display name before we have to close your account.
Thank you
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey "Spidey" (please change the name!)
The line "att2 = false" isn't an instance initializer. What this statement means is that the following would be illegal:

But code inside a method or a constructor can reference all member and static variables declared anywhere inside the class.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The line "att2 = false" isn't an instance initializer.
I believe it is, according to JLS 8.6. What else would you call one of those non-static unlabeled blocks inside a class but outside any method or constructor?
[B]What this statement means is that the following would be illegal:
[/B]
Those are instance variable initializers, JLS 8.3.2.2.
But the important difference between these two cases is not so much that one uses an instance initializer and the other uses an instance variable initializer. Rather, the
The rules for this are given in JLS 8.3.2.3.
Here's a variation on "Spidey's" original code which does not compile, because the usage of att1 inside the instance initializer is on the right side, and before att1 is declared.

I don't have a copy of Maughal & Rasmussen to see just how they state this, other than the single sentence quoted above. But based on that one sentence, it seems they may be in error. Unless they define what they mean by a forward reference, such that a reference on the left hand side of an assignment is never considered a forward reference. Someone with a copy of the book may want to check this and submit an erratum if there's no further clarification.
Note that JLS 8.3.2.3 allows one other special case of an initializer containing a non-LHS reference to a field declared later:

Basically this means that the compiler finishes parsing all outer-class field declarations before it tries to parse the details of a nested class.
I have a vague recollection that some earlier versions of the JDL might have had some bugs regarding forwsard references, so if you test on an older compiler you may get slightly different results. But for JDK 1.4.2 at least, the results I get seem to match what the JLS actually says.
[ October 18, 2003: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic