jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes compilation error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "compilation error " Watch "compilation error " New topic
Author

compilation error

AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
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



AshutoshP
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Bear in mind that enums aren't just regular classes with some syntactic sugar.
AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
@david
can you please explain me the order of execution of this program?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
@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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 27, 2005
Posts: 19725
    
  20

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

Joined: May 04, 2010
Posts: 32
@david
yes i tried the way including static which compiled fine.
thanks all.

 
 
subject: compilation error