File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes strange code from Jls about initialization of fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "strange code from Jls about initialization of fields" Watch "strange code from Jls about initialization of fields" New topic
Author

strange code from Jls about initialization of fields

Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
That's the code, chapter 8.3.2.3.

I comented the lines that causes compile time errors, but there are still 7 errors. This code states that a variable can be used in a static initializer before declared(one example of this is the use of static variable x). Is this right??? Is it ok to make x equals 100 in a static initializer while it will be declared just after this initializer??? I'm using j2sdk 1.4 on windows.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
This bug report refers to JLS 8.3.2.3 and class UseBeforeDeclaration:
http://developer.java.sun.com/developer/bugParade/bugs/4459133.html
Bug Id: 4459133
Synopsis: Forward reference legal or illegal?
Category: java:compiler
Reported against: 1.3
This is fixed in hopper (1.4.1) beta.
After I comment out the following lines in UseBeforeDeclaration, when I was using 1.4.0, I also got 7 errors just like you did. Now that I am using 1.4.1, the example compiles without error.
// int y = x + 1; // error - read before declaration
// j = j + 1; // error - right hand side reads before declaration
// int k = j = j + 1;
// int h = j++; // error - read before declaration

When I compiled this example using 1.4.0, I got the compiler error: illegal forward reference. Now that I am using 1.4.1, this example compiles without error.
[ March 10, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
This code states that a variable can be used in a static initializer before declared(one example of this is the use of static variable x). Is this right??? Is it ok to make x equals 100 in a static initializer while it will be declared just after this initializer???


This compiles under SDK 1.4.1 and Windows 2000.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is the Sun documentation that shows the forward reference bug was fixed in 1.4.1:
http://java.sun.com/j2se/1.4.1/docs/index.html
Click on Fixed Bugs
http://java.sun.com/j2se/1.4.1/fixedbugs/index.html
Click on Fixed Bugs
http://java.sun.com/j2se/1.4.1/fixedbugs/fixedbugs.html
1 vote 4459133 compiler Forward reference legal or illegal?
Click on 4459133
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
Thank you, really much!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: strange code from Jls about initialization of fields