static keyword has three uses in C++ language. You have just asked about one of them:
1) a static may be used as a modifier of a local variable like:
int some_function ()
{
static int n = 1;
return n++;
}
Unlike normal local variables, static local variable would remain alive and keep its recent value as long as program runs. it means that the variable n above would have different values in each call of the function some_function (). For the first call, it returns 1 and will be updated to 2. For the second call, it will return 2 and be updated to 3,...
2) static may be used as a modifier to a global variable or function (none-class member function or variable).
Suppose we have a project in some C/C++ programming environment, and we have the following variable and function definitions in one of the source files (source1.cpp):
// source1.cpp
static int xs = 0;
static void fs ()
{
//...
}
int xn = 0;
void fn ()
{
//...
}
Now, let's ask a question: We are writing the code for another file (source2.cpp). Can we accesses these variables and functions (xs, fs(), xn, fn() which are defined in source1.cpp) in our other source files (source2.cpp)?
The answer is YES for xn and fn() and NO for xs and fs(), because xs and fs () are static. We cannot access the static global variables of other source files, but we could access the non-static variables and functions by declaring them as external:
extern int xn; // OK
extern void fn (); // OK
Sure, compiler would generate an error for these declarations:
extern int xs; // Oops! compiler error because xs is static.
extern void fs (); // Oops! compiler error because fs () is static.
So, the static modifier marks a global variable and function as internal and hidden to the other source codes (or modules).
3) (SUBJECT of YOUR QUESTION) a static keyword may be used as a modifier to a class member variable or function:
class A
{
public:
int xn;
void fn ();
static int xs;
static int fs ();
};
For simplicity, I assumed all of these members to be public. Being static has nothing to do with a member's accessibility (being private, protected or public).
Let's compare the non-static members A::xn, A::fn () with the static members A::xs and A::fs (). The non-static members, A::xn, A::fn () , are considered to be the property and method of an object of class A:
A objA;
objA.xn = 10;
objtA.fn ();
The static members, A::xs, A::fs (), are considered to be the property and method of the class A itself, not ant particular object or instance of this class. They are in fact shared among all the objects and instances of class A. We cannot access them like xn and fn above as being a member of an object. We could only access them as the member of class:
A::xs = 4;
A::fs ();
To use a non-static member of a class, we must first create an instance of that class, then access that member for that object created. To use a static member we do not need any class instance.
A good example of when we need a static member for our class is when we want to assign am automatic id number to our created objects:
class SomeClass
{
private:
static int auto_number;
int id_number ;
public:
SomeClass () { id_number = auto_number++; }
};
// A static member variable must be defined and initialized externally:
Int SomeClass::auto_number = 0;
Now, each time we create a new instance of class SomeClass, an automatic unique id is created for that object.
I hope this info is useful.