Download the code
Static variables & methods are associated with the class rather than an instance of an object of that class.
Usually, the data contained within an object, belongs only to that instance.
If you want all instances of a class to share some data, use a static variable. A static variable belongs to the class, rather than an instance the class (object).
The example below uses a static variable to keep count of the number of Widget objects that have been created.
The Widget class contains a single static variable called _numberOfWidgets. No matter how many instances of Widget are created, there will be a single value associated with the _numberOfWidgets variable.
A static method is provided to set the value of _numberOfWidgets and a static method is provided to get the value of _numberOfWidgets. In this example, the constructor calls the set method and in turn, the set method increments the value.
In the TestWidgetClass below, a number of Widget instances are created. Note that the call to the get_numberOfWidgets method is made by referencing the class directly, rather than by instance of the class.
And the output is:
"Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)" ( http://bit.ly/KmbYd7 )
In a UML class diagram, Static variables & methods are represented with an underline.
Here's a cat to keep you calm:






