Static Fields and Methods
by, 03-02-2012 at 04:16 PM (2257 Views)
Methods and fields might be declared static. This is also possible for C++. Only one copy is present for the complete class when field is static, rather than one copy for all class instances. If class instance is not present even then fieldís copy is available. For example, given field addition in List class:
static int numLists = 0;
And, the given statement addition in the List constructor:
When new List object creation takes place, increment of the variable of NumList is done. Hence total created Lists are counted when a program is executed. All List instances access this variable and similar variable is accessible, not the individual copies.
Methods need to be static in case accessibility of non static field is not possible. Donít call non static method. In C++ Free function methods shall be static. Logically associated methods present with a specific class shall be static as well. These methods are involved in manipulation of the Static field class.
Public static field is accessible by usual notation, outside the class:
Instead of class object name, class name might be used: