Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compilation error

 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, when i tried to compile this code it says
illegal reference to static field(colorMap) from initializer
but , my doubt is static colorMap is initialized when the class is initialized .so colorMap is already initialized
then why it is illegal to use that is already initialilized
thanks in advance


 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear in mind that enums aren't just regular classes with some syntactic sugar.
 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@david
can you please explain me the order of execution of this program?
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Next time, please include the compiler error: The problem lies in the execution order. With enums, the enum values are initialized before anything else. So when your constructor runs because RED, GREEN and BLUE are created, the colorMap field simply has no value yet. This is something you can't work around - directly. Using a helper class will allow you to do this: The reason is simple - the map will now be initialized when the ColorMap class is loaded which is in the construction of Color.RED.
 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@prime thanks i tried the way you sugested that compiled fine

but, when i tried to implement it in this way i got the same error

in the below case the execution order should be like this
1)Initialize the enum values with the null-body constructor
2)initialize the staic variable colorMap
3) Initialize the instance-initializer where i am trying to access colormap.
but, it does not follow this execution order, i think.
can you please tell me the execution flow and which case it changes?

C:\javaprogram>javac Color.java
Color.java:10: illegal reference to static field from initializer
colorMap.put(c.toString(), c);
^
1 error



 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static

Edit: In case that isn't enough of a hint, explicitly make it static. "it" is left as an exercise for the reader. But note that similar static things are done on the Java enum tutorial page. So far I think you're just not aware of what an initializer block is.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AshutoshP Patil wrote:in the below case the execution order should be like this
1)Initialize the enum values with the null-body constructor
2)initialize the staic variable colorMap
3) Initialize the instance-initializer where i am trying to access colormap.
but, it does not follow this execution order, i think.
can you please tell me the execution flow and which case it changes?

Actually, instance initializers are run when the instances are created. With enums that's still before the static field is declared. By using a static initializer block like David suggested the filling of the map is postponed until after the map is declared.
 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@david
yes i tried the way including static which compiled fine.
thanks all.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic