Here's an example of a static method that is used in a web application. As you can see, the String[] allergensArr gets insantiated each time that this method is called. It's threadsafe since it's in a static method but it's an expensive call.
What are some other ways that the allergensArr[] can be used so that it's not instantiated each time the method is called.
I was considering the following options.
- Have a static constructor that initialzies a static final String[]
- Use a singleton (though this would block a lot of people)
This will be a constant array that will not change in the lifetime of an instantiated server.
public class UserHealthConcernsManager { public static String[] getAllergensFlag () { String[] allergensArr = new String[12]; allergensArr[0] = "x"; allergensArr[1] = "y"; allergensArr[2] = "w"; _SNIP_ return allergensArr; } }