Is NumberFormat.getInstance guaranteed to create a new instance?

Viewed 3825

Consider the following code:

NumberFormat format = NumberFormat.getInstance();
format.setMinimumFractionDigits(spotDecimalPlaces);
format.setMaximumFractionDigits(spotDecimalPlaces);

Is it "safe"? Is NumberFormat.getInstance() guaranteed to return a new NumberFormat object each time?

Or is there a possibility that getInstance() returns the same instance? (in which case this code would affect everywhere else in the JVM that happens to use getInstance...)

Looking at the source code it seems like it returns a new instance each time. The JavaDoc is frustratingly vague on the matter.

If the above code really is "safe", then it seems to me that getInstance() is a poor name for this method - that it should have been called createInstance().

Is NumberFormat.getInstance() guaranteed to always return a new instance?

4 Answers
Related