static Keyword :
- static keyword is used to create static data member. Memory is allocated only once for static data members of a class, no matter how many instances are created.
- A static member is shared by all objects of the class.
- All static data is initialized to zero when the first object is created, if no other initialization is present.
- Static data members are not part of objects of a given class type.
- As a result, the declaration of a static data member is not considered a definition. The data member is declared in class scope, but definition is performed at file scope by redeclaring the static variable using :: operator.
- Static data members can be referred to without referring to an object of class.
- For the static member to exist, it is not necessary that any objects of the class type exist
- Static members can also be accessed by class name using the member-selection (. and –>) operators.
- static member function can only access the static data member or static member function and any other functions from outside the class.
- Static member functions have a class scope and they do not have access to the this pointer of the class.
Following examples illustrates all the above properties of static :
using namespace std;
static int y;
static void display_y()