Static Variable:
In java when a variable declared as static, it is called a class variable and all objects (instances) share the same variable.
Static Method:
A static method always accesses static data and it also belongs to class not instance. Hence the static method is called with the class name not object name. It cannot be referred to like this or super keyword.
Program:
Output:
private static int cnt - is a private member for class StaticVar and it is static too, it means the variable cnt can not be accessed outside of the class (due to private) and it is allocated once (due to static).
getCounter() - is a static method, which is calling with class name Item.
need an explanation for this answer? contact us directly to get an explanation for this answer