Saturday, December 08, 2007

How to find the number of instances created programatically.

class InstanceCreationClass {

public static void main(String[] args) {

ObjectCount one = new ObjectCount();
ObjectCount two = new ObjectCount();
ObjectCount three = new ObjectCount();
ObjectCount four = new ObjectCount();
ObjectCount five = new ObjectCount();

System.out.println("Result: " + ObjectCount.i);
}
}



class ObjectCount {

static int i;

public ObjectCount() {
i++;
}
}

0 Comments:

Post a Comment

<< Home