import java.util.Date;

public class FixedTimeStampedObj {
    Object payload;
    Date timeStamp;

    // To avoid publishing this in constructor, make it private and
    // do not assign to cache
    private FixedTimeStampedObj(Object o) {
	this.payload = o;
	this.timeStamp = new Date();
    }

    // Static factory method is what users use to create objects now
    public static FixedTimeStampedObj newInstance (Object o) {
	FixedTimeStampedObj tso = new FixedTimeStampedObj(o);
	FixedTimeStampedObjCache.lastObjCreated = tso; 
	return tso;
    }
    
    public Date getTimeStamp() { return timeStamp; }
    
    public Object getPayload() { return payload; }

    // Driver: what will errorCount be when printed?
    public static void main(String[] args) {
	int errorCount = 0;
	int iterations = 10000;
	Thread t;
	
	for (int i=0; i<iterations; i++) {
	    t = new Thread(new Runnable() {
		    public void run() { new FixedTimeStampedObj(new Object()); }
		});
	    t.start();
	    if (FixedTimeStampedObjCache.lastObjCreated.getTimeStamp() == null) {
		errorCount++;
	    }
	}
	System.out.println(errorCount);
    }
}
