• 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
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

Discussion about Java enums

 
Ranch Hand
Posts: 53
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have been working very long with java but i did not paid attention to understand java Enum behavior this point of view. This thread is not about asking question , but also share my experience which i have learned over the period.

Below is my java class.


1-What happened when we decomplied enum class


These two methods are added by java compiler automatically.

Now it is turn to ask my question , I have observed when we called values static method it created new Arrays with clone of all the enums. If i would call values method 100 times then it will return 100 new Arrays objects.
I know why java is doing this so the if any one by mistake change the value in Array it will not have impact else where.

Which mean if you call values method  again you will get the correct value.



Out put is

366712642 1829164700 2018699554 1311053135 118352462

All the properties are enum is to behave like constant , but values method work differently. I am thinking should we not have one more method which always return the same array instance irrespective of how many time this method is being called ?
This way at least we should not create unnecessarily array object. I know this is not the first time some asked this.

I feel we should have one more method which help us to get the same array object , if we want to make sure we are not breaking anything we can have one parameter which say if you want same object or behave like values method.(if you are not sure)
Please let me know your opinion.



Thanks,  


 
Sheriff
Posts: 67762
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

prateek shaw wrote:I am thinking should we not have one more method which always return the same array instance irrespective of how many time this method is being called ?
This way at least we should not create unnecessarily array object. I know this is not the first time some asked this.


Seems like a micro-optimization to me, and certainly not worth creating yet another method for. Have you observed problems that stem form the creation of new arrays?
 
Sheriff
Posts: 22907
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

prateek shaw wrote:I am thinking should we not have one more method which always return the same array instance irrespective of how many time this method is being called ?
This way at least we should not create unnecessarily array object. I know this is not the first time some asked this.


You already answered this question yourself:

I know why java is doing this so the if any one by mistake change the value in Array it will not have impact else where.


Since arrays are not immutable, one could easily modify the return value and all other code that expected on values() to return the correct values would break. "Fixing" the array each time the method was called would a) get rid of most of the performance gains you want to get, and b) is not thread safe (or the values() method becomes even slower in a multi-threaded environment).

Now the main question should probably not be "why doesn't values() return a shared array", but instead "why doesn't values() return a shared immutable collection"? Enums were added in Java 5.0, when the Collections Framework was already mature, so making it return an array was probably a mistake.
 
prateek shaw
Ranch Hand
Posts: 53
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replay. Yes forgot to asked why its return Array not Collection.

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An enum is a special type of data type which is basically a collection (set) of constants. Java enum is just a fixed number of constants that the developer defines while writing the code.Enums can actually be much more than mere constants, having their own attributes and methods.
 
Marshal
Posts: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope this is an exercise in working out what happens if you extend the Enum class. Enum is not designed to be extended by users, but all enums implicitly extend if.
 
prateek shaw
Ranch Hand
Posts: 53
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I hope this is an exercise in working out what happens if you extend the Enum class. Enum is not designed to be extended by users, but all enums implicitly extend if.



That example class was recompile version of java class
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic