Class members modifier: static

Members (fields and methods) of a class may be prefixed by the keyword static to distinguish them from instance (i.e.,non-static) members.
Static members belong to the class they are declared in but make no part of any instance of that class.
When the class is loaded, static variables (also called class variables) are initialized either explicitly by an initialization expression or given default values, these values are not part of the state of any object. In other words, static variables are not instantiated if an instance of the class is created.

There is no notion of object associated with static methods (also known as class methods), therefore a static method in a class can't access instance (i.e.,non-static) members. A static method can access directly other static members in the class. However static methods can always use a reference of the class' type to access its static and instance members.

Clients may access static members through the object reference of the class or simply by using the class name. Anyway, the class need not be instantiated to access its static members.

No comments: