All Articles

getInstance or newInstance?

Don’t use getInstance() factory methods that in fact return new instances on every single call. There’s a better name for these cases: newInstance(). Or createInstance(), or of(), or from(), or anything that makes intentions (semantics, performance and a bunch of other things) clear.

Keep getInstance for singleton factories, maps, cached instances or other such cases. Contrast Locale.getInstance(algorithm) with Array.newInstance(type, length). Simple.