Are we talking about passing a List to be populated by a method? That in itself has much nicer alternatives; I think this is obvious, so I will assume that you are *returning* a List from a method. The dangers here are a bit more subtle. Design By Contract (which is what you are doing even if inadvertantly) mandates that all contract participants (parameters and return types) are not mutable types. So how do you achieve what you want to achieve? Well simply put, the java.util.List should have been split, at least into mutability and immutability (sorry Josh
). The java.util.List interface violates the fundamental rule "two or more methods on an interface must be symbiotic (exist together or not at all)". Quite clearly, mutability and immutability does not exist symbitiocally. It would have been nice to return an immutable List, and it's quite possible by using delegation i.e. defining your own interface that is a subset of java.util.List containing only operations that do not modify the List, then delegate to an underlying instance of java.util.List. However, don't fall into the same trap - split out all operations that are not symbiotic. I should point out that declaring to throw UnsupportedOperationException is a suboptimal solution i.e. a workaround - avoid it where possible and use a statically typed language to enforce type-safety at compile-time as much as possible.
I did this so often that I decided I'd write my own sequentially ordered container. This example is certainly far from optimal and this has more political reasons than technical ones; for example, the length() method should have (and has been but it is not public code) split out into an interface Lengthable{int length();}, and there are a few other examples of "breaking the rules". Nonetheless, here is the type. You'll note that it is both immutable, and does not perform any data copying when an operation is invoked that returns a new sequence. That is to say, when you append a sequence containing "b" to a sequence containing "a", this does not mean that "a" and "b" are copied into a new sequence; merely an interface is returned that provides a new view of the same data.
http://www.jtiger.org/javadoc/org/jtiger/framework/Sequence.html Anyway, I better stop ranting.
[ August 11, 2005: Message edited by: Tony Morris ]