see also JavaSingleton
public class Registry { private static List register = new ArrayList(); public static void register(Object obj){ register.add(obj); } public static boolean isRegistered(Object obj){ return register.contains(obj); } }The above Registry class makes use of a private static List to hold objects. There are many cases where this is fine. However, What would happen if Registry were loaded by two different ClassLoaders. I think there would be two instances of the class, and therefore two instances of the register list. -- EricHerman
More generally, since static methods are not polymorphic, you may find using them to encourage a less objecty (and perhaps more smelly) style.
There's also the trap that I fall into, that static members are not inherited. So if you have a FooRegistry which extends Registry, it needs its own static ArrayList and you have to re-write the register etc. methods not in terms of super().
Though personally I don't find these kinds of "static classes" objectionable, especially where they're used for Factory or registries or in-general keeping track of instances of a set of related classes. I'm also happy that there exists the Collections class (I like this 'pluralization' naming convention, too) in the Java library for its methods like #sort(), but there are others there - especially things like #unmodifiableSet() that would benefit from MoveMethod refactoring.
In ZoIsite? I use "static classes" to implement a kind of Facade - that's how I think of it anyway. Whenever I write one of these, I always include something like
private ClassName() {} // static class-- KarlKnechtel
Interesting. Where would you move 'Collections.unmodifiableSet(<Set>)' to?
'new UnmodifiableSet?(<Set>)' That's what happens behind the scenes in Collections.unmodifiable<Blah>() anyway.
Something like that. and Collections.singleton(Object) - I think that was the name; the one that makes an immutable set containing just the specified Object - definitely needs that kind of treatment. Say, new UnmodifiableSet?(Object[]) with a one-element array.
Static methods in Java aren't really class methods. They're really global methods, since they're bound at compile-time and can't be overridden. In the past I've found myself writing bizarre hacks to get around this limitation in language design, though these days I just use another language if I have a choice. -- francis
CategoryComputerLanguage?