There are several patterns, such as Singleton, which govern creating only one object. The easiest way I can thinking of creating only one object per 'session' is to use a private local variable as a reference, and only create an object if that reference is null.
McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
If you really want one instance per thread, ThreadLocal is a utility class in the library. A ThreadLocal variable holds the real object you want to deal with and assures there is one per thread. It works rather like a Map where the key is the current thread.
Jeremy's example doesn't really address threading. If we make an instance of C2 and pass it to two threads, the two threads share the same instance of C1, too. Something like this would have one instance of C1 per thread:
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi