• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

enum "initialization circularity"?

 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Straight from java laguage specification, this code won't run because:

Static initialization of this enum type would throw a NullPointerException because the static variable colorMap is uninitialized when the constructors for the enum constants run. The restriction above ensures that such code won't compile.



What does that mean? I don't understand. What is the problem with this code?
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1:- Enum must not declare in method.. - So taking out enum from main method..



2:- colorMap is static member.. so you should refer it as Color.colorMap.. and your code will compile fine also run fine....


But if you add one line in main() then below quote come in picture..




Static initialization of this enum type would throw a NullPointerException because the static variable colorMap is uninitialized when the constructors for the enum constants run. The restriction above ensures that such code won't compile



 
Greenhorn
Posts: 15
BSD
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Illegal reference to static field colorMap inside of the constructor

Enum constructors cannot access the enum static fields. However, they can invoke the enum’s static methods that access the static fields. The problem is static initialization has not been done at the time the enum constructor constant constructors are invoked, so using static methods will just see zeros. Constructors can also directly access static final in-lineable constants known at compile time.



Static variables have to be assigned to their values when a class is first called. That is called static initialization.

You may therefore try to access the static field inside the constructor, but... (hint: you will only get a NullPointerException)
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.. Color.colorMap will be null..


If try to compile and run following code it will run .. because you are just declaring Enum



but when you try to use Enum.. you will get null Pointer at run time..

reply
    Bookmark Topic Watch Topic
  • New Topic