File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Forward refs - revisited Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Forward refs - revisited" Watch "Forward refs - revisited" New topic
Author

Forward refs - revisited

Neil Mac
Greenhorn

Joined: Oct 03, 2003
Posts: 7
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

Joined: Aug 20, 2001
Posts: 1817

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


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Forward refs - revisited