If a member in a class is declared as static, the member is called a quiescent member. The members in the class are either static or non-static. Generally, static members belong to the class, and non-static members are instances of the class-objects.
The following sample code demonstrates how to declare static and non-static members.
Program Listing 10-5:
Using System;
Class Test
{
int x;
static int y;
void F () {
x=1;//correct, equivalent to this.x=1
y=1//correct, equivalent to Test.y=1
}
static void G () {
x=1;//error, no access to this.x
Y=1//correct, equivalent to Test.y=1
}
static void Main () {
Test t=new test ();
T.x=1; Correct
T.y=1; Error, static member Test.x=1 cannot be accessed in an instance of a class
;//Error, non-static member Test.y=1 cannot be accessed by class
;//Correct
}
A non-static member of a class belongs to an instance of a class, and each instance of a class creates an area in memory for non-static members. The static members of the class are owned by the class and are shared by all instances of the class. No matter how many replicas this class creates, a static member occupies only one area of memory.