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.