This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use a variable before it's declaration

 
westwindode west
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, everybody.
This following content and a piece of code are from sun website:
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#287406
It's about the "Restrictions on the use of Fields during Initialization". It also says :"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."
If a variable is used before it's declaration, but the variable is on the left side of an assignment (like line 3 of the code), according to the book, it will compile and run well. But when i compile it, java1.4 JDK reports:
(UseBeforeDeclaration.java:3: illegal forward reference
x = 100
Did the book make a mistake or it's b/c the book was with different version of java?
class UseBeforeDeclaration {
static {
x = 100; // ok - assignment
int y = x + 1;
// error - read before declaration
... some lines of codes
}
static int x;
}
Thanks a lot...
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check out the following discussion which handles the same topic:
regarding forward referencing and 'implicit narrowing conversion
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I think this code is a bit different from the situation discussed in the link. This code still does not compile under 1.4.1, and that is correct according to a literal interpretation of the JLS here. The key is to realize that

is a use of x which is not on the left hand side, and this occurs before the declaration of x. It doesn't matter if there was another, previous line

Even though common sense tells us that this line has made a definite assignment to x and so subsequent code should be able to use x, the JLS rules cited here make no mention of the effect. So forget common sense - "int y = x + 1;" is a use before declaration, according to the JLS.
 
westwindode west
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both...
I'll try version 1.4.1 tonight (rather than 1.4.0), and I suppose the line x = 100 might work then...
meanwhile, i now understand why int y = x + 1 would not compile...
It's a nice experience to find clarity in the middle of confusion
westwindode.
 
westwindode west
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by j2sdk1.4.1_01 the line i = 100 does works...

thanks
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic