• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Use of field before its declaration - JLS

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JLS - 6.3
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:
1. The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.

2. The usage is not on the left hand side of an assignment.
3. C is the innermost class or interface enclosing the usage.
What does (respectively static) mean here ?
Could someone explain this excerpt from JLS with examples because the example given in the JLS is not correct !
Thanx :-)
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Err... after some searching, I found the section you're apparently looking at in section 8.3.2.3 of the JLS 2nd edition. The use of "repectively static" is quite unclear here, and seems very poorly worded. (Contrary to the usual pattern for the JLS, which is usually very precisely and correctly worded, just hard to understand.) In this case, my guess is that it's an attempt to compress the following statement:
<blockquote>
The declaration of a member needs to appear before it is used only if one of the following two sets of conditions is true:
  • The member is an instance field of a class or interface C and all of the following conditions hold:
    1. The usage occurs in an instance variable initializer of C or in an instance initializer of C.
    2. The usage is not on the left hand side of an assignment.
    3. C is the innermost class or interface enclosing the usage.
    4. The member is a static field of a class or interface C and all of the following conditions hold:
      1. The usage occurs in a static variable initializer of C or in a static initializer of C.
      2. The usage is not on the left hand side of an assignment.
      3. C is the innermost class or interface enclosing the usage.

      4. </blockquote>
 
Deepak M
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
Err... after some searching, I found the section you're apparently looking at in section 8.3.2.3 of the JLS 2nd edition. The use of "repectively static" is quite unclear here, and seems very poorly worded.</blockquote>


I was referring to the Draft edition of JLS, hence got mixed up with the sectoins.
As per the example given in the specs I get an compile time error with the following code - both javac as well as jikes
class UseBeforeDeclaration {
static { x = 100; }
static int x;
}
The example in JLS is apparantly wrong !
Could someone give the reasoning for this ?
Thanx :-)

[This message has been edited by Deepak M (edited July 14, 2000).]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it looks like this is a new rule in 2nd edition. The original JLS said that a forward declaration like this should give an error, period, without mentioning that an error should only occur if the reference was not on the left hand side of an assignment. So the JDK is in compliance with JLS1 but not JLS2. Since the JLS2 mentions this case very specifically, I'm guessing that this is an intentional redefinition of the language, and they just haven't released a compiler yet that implements it. Remember, JDK 1.3 came out before JLS2. (Well actually 1.3 beta came out before JLS2 draft, which I think was before 1.3 standard, which was before JLS2 standard.) Basically I think they're confused here - perhaps you should submit a bug report & see what they say?
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add to the confusion, apparently the earlier/older version of JLS said "The static initializers and class variable initializers are executed in textual order and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope. This restriction is designed to catch, at compile time, circular or otherwise malformed initializations."
and there are bugs 4281572, 4312145 raised for recent javac based on this.
The bug description goes like this...
"New javac (jdk1.3.0K) compiler doesn't report about illegal forward reference to static variable in static inializers before variable's declaration. Note that jdk1.3.0K oldjavac compiler correctly reports the error."
Looks like compiler team and JLS writers at Sun dont talk to each other as often as they should. (:-0 )
vivek
 
Deepak M
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are absolutely right vivek
As per JLS 8.3.2.3 and the example given there
class Test
static {
x = 100; ok - assignment
}
static int x;
}
The above code should compile. But it doesn't ! Whyzzat ?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See followup discussion here.
 
reply
    Bookmark Topic Watch Topic
  • New Topic