• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Help! Field initialization sample in JLS

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,here is the samle code from JLS 8.3.2.3 regarding field initialization, I already comment-off what author marked "error", but it still have problems of "forward reference". I also include error from javac. Can anybody explain? Does the author wrong, or Sun's compiler does not strict follow JLS?

errors:

you can see the original here:
[url = http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#287406
[/url]

[This message has been edited by Haining Mu (edited June 19, 2001).]
[This message has been edited by Haining Mu (edited June 19, 2001).]
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Haining,
The author screwed up! Static blocks are executed at class load time in the order of top down. Since you are not declaring x before you are trying to assign it you will get errors. If you move the static int x; declaration above static block it will not complain about that.
I would think that you would have the same problem with the instance declarations.
Regards,
Manfred.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me get this straight.
Sun writes some sample code just to show you what NOT TO DO, and you are trying to get it to COMPILE? They NEVER said that the thing would ever work. They ONLY said "Don't do this".
Perhaps you would be better of just absorbing the intent of the example.
 
reply
    Bookmark Topic Watch Topic
  • New Topic