File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enum Constructor doesn't access non-final static variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enum Constructor doesn Watch "Enum Constructor doesn New topic
Author

Enum Constructor doesn't access non-final static variable

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Enthware question had the following as an option for "Identify the valid enum declarations"



The reason the above is wrong is because,
Unlike a regular java class, you cannot access a non-final static field from an enum's constructor.


What is the logic for creating this restriction in Java programming language?


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

All enum constants (i.e. DOG, CAT and FISH in this case) are implicitly public, static and final. This means that they'll be initialized before static non-final fields of the class (like prefix in this case). So the field will not be initialized when you try to access it from the constructor...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Sandra, What will be the out of this code snaps?

This is the reason.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
@Abimaran:

I ran copied and pasted the code into a blank document and name it Pets.java




It compiled without error

However, when I replaced the following code with



I received the following compiler error:

Pets.java:7: illegal reference to static field from initializer
Pets(String s) { name = prefix + s;}
^
1 error



ankit wrote:All enum constants (i.e. DOG, CAT and FISH in this case) are implicitly public, static and final. This means that they'll be initialized before static non-final fields of the class (like prefix in this case). So the field will not be initialized when you try to access it from the constructor...


I think I understand. Statics are initialized first and they are at the top of the Enum definition. Hence the non-final static variables have not been defined yet and should not be accessed from enum's constructor.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Sandra, If you ran my above code snaps, you can find the different. The Enum Constance instantiated before the static initialization block run. So, with that, same applies to the static non-final variables also initialized after instantiating the Enums, So, you can't use them in the Constructor of the Enum. Ankit already mentioned it, and i just code it.
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Hi, I'm not sure if you guys don't like it when old threads are revived or not, so it was a 50/50 chance if wether I should make a new thread on the same topic or just respond here...

If I understand Ankit's statement above, static final variables will actually have been initialized when the constructor of an enum executes. Is that right, or are they still not initialized at "constructor time" even if theyre marked final?

Secondly, I find it odd that the above code generates a compiler error, while the following does not...



As you can see I "bypassed" the compiler error by calling a method instead of accessing the static variable directly from the constructor. Somehow this doesn't cause a compiler error and the code runs fine, except ofcourse that it doesn't work as it should
And by that I mean, if you run it you will find that the println() will actually print "0". Is there any logical explanation to why this doesn't cause a compiler error, just as if I had accessed the variable directly from the constructor, or should I consider it a bug that it is able to compile?

// Andreas
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Just a small bump (hope thats ok?), cus I'd really like to get some feedback on this...

// Andreas
Hareendra Reddy
Ranch Hand

Joined: Jan 09, 2011
Posts: 173

Hello Andreas,
Actually what you are doing is making the illegal forward reference legal,
and you can read more about it here...

but still i wonder why the values is zero instead of 3...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enum Constructor doesn't access non-final static variable