• Post Reply Bookmark Topic Watch Topic
  • New Topic

Forward reference using class name  RSS feed

 
Ambapali Pal
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an example from JLS 8.3.2.3 Restrictions on the use of Fields during Initialization

Can anybody explain why at line 1 is ok i.e. if a variable is read using its ClassName before its declaration why does it compile.
Thanks in advance,
Ambapali

[ Jess added UBB [code] tags to preserve whitespace, check 'em out! ]
[ January 10, 2003: Message edited by: Jessica Sant ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a mystery to me. As you note, JLS 8.3.2.3 shows an example where use before declaration is OK if the use is with a qualified name - but I can't find any explanation of this rule other than the example. The example appears to contradict the stated rules elsewhere. I suspect that there is a rule somewhere explaining this, but it's cleverly hidden. However it's also possible this problem was never noted; perhaps there was a rule in some earlier draft of the JLS, and when the rule was removed they failed to also remove this reference to it? Some of the rules in 8.3.2.3 were not implemented correctly until JDK 1.4.1 (see this bug), which is evidence that this area of the JLS may not have been carefully scrutinized.
I'd say if no one can find an explanation, we should report it as a bug in the JLS, and see what they say.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Come to think of it, this has nothing to do with certification really, so I'm moving it to Java in General - advanced. Perhaps someone will note something we missed.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Part of the trick in this situation seems to be the term 'used'. The JLS says a field can't be 'used' before declaration, but the examples distinguish between reading and writing. Reading is shown to be illegal while writing is apparently permitted. The JLS does specify (top of 8.3.2.3) that usage is only illegal when "The usage is not on the left hand side of an assignment." That is, only read access is illegal.
It looks like the JLS is consistant, just not too clear.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I believe I got that part. This has been discussed recently here and here. But the problem here is the specific issue of using a qualified name. The example in the JLS here contains an example with the comment
Moreover, this line of the example does indeed compile under JDK 1.4.1 (once you comment out the other lines which are given as examples of illegal code). But if you delete the "UseBeforeDeclaration." the code does not compile anymore, indicating that indeed a simple name here is illegal, but a qualified name is OK. The question is, why? Where in the JLS does it provide any justification for allowing this use-before-declaration for a qualified name, but not for a simple name? A litleral reading seems to indicate that both uses should be illegal. A common-sense reading indicates that since the variable has been definitely assigned prior to the usage, the usage should be legal. But the JLS doesn't actually support this common-sense interpretation as far as I see, and even if it did - what does simple-vs.-qualified name have to do with it?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!