• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Forward refs - revisited

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic