Make noOfInstances static as follows,
static int noOfInstances;
and you donβt need to call e1.noOfInstances , instead you can call Exmp2.noOfInstances
An instance (non-static) variable is copied to objects, while static variables are not copied to the object. static are at the class level. Each object can see it.
source share