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?

6
  • 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"? Commented Dec 23, 2015 at 10:55
  • There are too many possible answers, ranging from enterprise stupid policy to hard habits or worse. How could we know? Commented Dec 23, 2015 at 10:58
  • 1
    Maybe some framework require that if you wish to use the values, for example an UI framwrok? Commented Dec 23, 2015 at 11:00
  • i agree with Krzysztof Cichocki. Frameworks needs getXxx methods to be generic :) Commented Dec 23, 2015 at 11:04
  • not so strange: it is usefull for subclasses: see my answer. Commented Dec 23, 2015 at 11:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 See: 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.
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?

1 Comment

@assylias totally right. I change my answer according.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.