Let's say I have a base/main abstract
class: AbstractObject
. AbstractObject
is extended by two classes: Object1
and Object2
.
Those two templates have many different classes extending from them also, for instance, Object3
, Object4
, and Object5
are inheriting from Object1
, and Object6
, Object7
are inheriting from Object2
.
I want each separate class (Object1
, Object2
, ..., Object7
) to have a counter that counts the number of instances made of it.
I thought I could use some public static
variable counter
, but I am not sure how to declare it in AbstractObject
, so none of my objects will copy the code. Any thoughts?
abstract class AbstractObject {
...
public static counter = ?
}
class Object1 extends AbstractObject {
...
}
class Object2 extends AbstractObject {
...
}
class Object3 extends Object1 {
...
}
class Object4 extends Object1 {
...
}
class Object7 extends Object2 {
...
}