1

In the project I'm working on I found a strange code:

public enum Service {
    ...
    private static final Service[] values = values();

    public static Service[] getValues() {return values;}
}

Do you have any idea why implementer added his custom method instead of using values() method everywhere? I know, values() method is generated in compile-time, does it affect anything?

awfun
  • 2,316
  • 4
  • 31
  • 52
  • 5
    He has been writing too many JavaBeans? It was midnight and he should have gone home earlier? He thought `values()` was slow and wanted to store the result in a variable to "improve performance"? – assylias Dec 23 '15 at 10:55
  • There are too many possible answers, ranging from enterprise stupid policy to hard habits or worse. How could we know? – cadrian Dec 23 '15 at 10:58
  • 1
    Maybe some framework require that if you wish to use the values, for example an UI framwrok? – Krzysztof Cichocki Dec 23 '15 at 11:00
  • i agree with Krzysztof Cichocki. Frameworks needs getXxx methods to be generic :) – halil Dec 23 '15 at 11:04
  • not so strange: it is usefull for subclasses: see my answer. – guillaume girod-vitouchkina Dec 23 '15 at 11:47
  • When you process enums or annotations you always get a new copy of an array and there we really could use immutable arrays. Maybe future versions of Java will have immutable array (or value typed array, which would be immutable), so this won't be a problem anymore. They are now working on this (Project Valhalla). – Claude Martin Dec 23 '15 at 11:59

2 Answers2

4

It is because the normal Enum.values() creates a new array every time to make sure the results of the call are always consistent.

This code is removing that and just calling it once. It may be because the coder thought that this could lead to memory leaks/thrashing.

It is a code smell because you could do Service.getValues()[2] = xxx; and corrupt the array for all users.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • This is very often called method, and creation of a new array would add work for GC. Seems like that. Could you please provide any resource to check how values() method works? – awfun Dec 23 '15 at 11:42
  • @awfun See: http://stackoverflow.com/questions/32799282 As this answer notes, each call creates a new array. But if you have, say, 10 enum constants, it means you create ca. 50 bytes of garbage at each call. Unless you call the `values` method millions of times it is not going to make much difference. Performance-wise, `values()` takes about 3x more time than `getValues`, i.e., on my computer, 10 nanoseconds instead of 3. There again, it is not significant unless your application calls it all the time. And as noted `getValues` introduces a potential bug so the trade off does not seem worth it. – assylias Dec 23 '15 at 12:38
0

I already did that once with a class because it is the simple way to solve this problem:

  • you have a class A, with methods, and static data (parameters, ...)

  • you want subclasses B1, B2, which use same methods (of B or of A), with its own parameters or parameters of A (depend on B1, B2, ...)

You cant subclass static variables, then you get them by a method you can subclass. And by default, you get A static parameters.

It is not possible with Enum (can not be extended), but perhaps coder wanted to extend. one usefull post: Can enums be subclassed to add new elements?

Community
  • 1
  • 1