wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Illegal Forward Reference - compiler not consistent with JLS - Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Illegal Forward Reference - compiler not consistent with JLS - Help" Watch "Illegal Forward Reference - compiler not consistent with JLS - Help" New topic
Author

Illegal Forward Reference - compiler not consistent with JLS - Help

R Arun
Ranch Hand

Joined: Apr 20, 2002
Posts: 33
From the JLS:

8.3.2.3 Restrictions on the use of Fields during Initialization
The declaration of a member needs to appear before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:
The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
The usage is not on the left hand side of an assignment.
C is the innermost class or interface enclosing the usage.
A compile-time error occurs if any of the three requirements above are not met.

However the following code does not compile using Sun's javac compiler (1.4.0):

In fact there is a specific example in the JLS that shows the above code to be legal - see the first line of the static intializer in the section referenced above where x = 10; is used before x is declared.
What's happening?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
This is a bug in javac. It doesn't follow JLS 8.3.2.3 There was a post in the Ranch showing a bug in the Database at SUN. You can search here or going directly to the database to see it.


SCJP2. Please Indent your code using UBB Code
R Arun
Ranch Hand

Joined: Apr 20, 2002
Posts: 33
Thanks Jose.
 
wood burning stoves
 
subject: Illegal Forward Reference - compiler not consistent with JLS - Help
 
Similar Threads
when static variables are initialized
Initialization
Forward variable referencing.
write operation before declaration...
why is this code showing compile time error?