File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum Constructor doesn't access non-final static variable

 
Sandra Bachan
Ranch Hand
Posts: 434
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandra, What will be the out of this code snaps?

This is the reason.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a small bump (hope thats ok?), cus I'd really like to get some feedback on this...

// Andreas
 
Hareendra Reddy
Ranch Hand
Posts: 173
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic