Class Variables & Methods
Instance Variables
Ordinarily, attribute values belong to individual objects and methods describe the object’s individual response to messages.
An example is that two alarm clocks can each have their own type.
Class Variables
Using the keyword static you can declare that an attribute (or method) belongs to the class. This means that it is shared among all instances.
An example would be that all alarm clock from your manufacturer have the same make.
Static Variables & Methods
-
The values of
staticvariables can be read and modified by all instances of the class, as well as from withinstaticmethods.Changing the
makeof one clock will affect all other clocks. -
staticmethods cannot access instance variables or instance methods but only otherstaticmembers.One clock cannot directly find out the time of another clock without a service.
-
staticmethods can be called directly on the class without instantiating an object. E.g.AlarmClock.ring();.staticandpublicmethods can be called without making an object.