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,