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

strange code from Jls about initialization of fields

 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, really much!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic