Static variables and static functions:-
The keyword 'static' means that only one instance of a given variable exists for a class.
Static variables are used to define constants because their values can be retrieved by invoking the class without creating an instance of it.
Static variables can be initialized outside the member function or class definition.
Unlike other member variables, only one copy of the static variable exists in the memory for all the objects of that class.
Static functions can access only static variables.
Static functions exist even before the object is created.
Example:-
class Test
{
public int numberI;
public static int numberII;
public void MethodI()
{
numberI = 10;
numberII = 20;
}
public static void MethodII()
{
numberI++; // Error Static Method only static Member Access
numberII++;
}
}
0 comments:
Post a Comment