The static members are elements of a class that don't need to belong to any instance class. This is, elements that exist without the need of the creation of an instance.
For example: there is a class called Clocks, that is a virtual representation of the real world clocks for a market. But it is neccessary to register the number of clocks selled. So, we can have an independient int element, selled, which will exist inside the class.
Because coding is better than explaining, let's just write a small sample:
// // Static elements inside a class // using System; public enum ClockType { Analogic, Digital, Modern, Cucu }; public class Clock { // Constructor public Clock ( ClockType theType ) { this.type = theType; // For each new Clock, // the static member selled will // increment in 1 selled++; } // Read-only type Property public ClockType Type { get { return this.type; } } // Private element type private ClockType type; // Static element // Doesn't need an instance class to be used public static int selled = 0; } public class Tester { public static void Main () { // // Let's create three clocks for selling // Clock[] clocks = new Clock[3]; clocks[0] = new Clock(ClockType.Analogic); clocks[1] = new Clock(ClockType.Digital); clocks[2] = new Clock(ClockType.Cucu); // Now let's print the Clocks selled foreach ( Clock clock in clocks ) { Console.WriteLine("Clock is type {0}", clock.Type); } // Finally we use the public static field Console.WriteLine("{0} Clocks selled.", Clock.selled ); } }
Now let's review the important sections. First of all, observe the field:
public static int selled = 0;Note the reserved keyword static; it goes after the access modifier but before the type of the field. This word tells the environment to reserve space in memory, because it is created without the need of any instance class. Because of this, we initialized it in the beginning ( no class memebers initialization ). It is possible to avoid assign a value for a static field, but take care when trying to access its value before any initialization.
The next important section is the class constructor:
// Constructor public Clock ( ClockType theType ) { this.type = theType; // For each new Clock, // the static member selled will // increment in 1 selled++; }The important line is the last, selled++. This causes to increment the selled private field selled. How is it possible to recognize between a static and a non-static field? Well, most of times the compiler will look for a field, ignoring if it is or not a static member. There's no problem if, for example, you only write type=theType;. Note that it is a good idea to work with all the non-static field as this.something rather than something, because of a good documented code ( remember than this referes to the current instance ). Also note that it is impossible to declare a pair of field with the same name, event if one is static and the other not.
Next, if we want to use a static member of a class, we have to write: first the name of the class, a dot, and the name of the member ( if public ). In this case, our class is Clock, and our current member selled, so for accessing it we write Clock.selled:
Console.WriteLine("{0} Clocks selled.", Clock.selled );
Compile and runt it; you should get:
Clock is type Analogic Clock is type Digital Clock is type Cucu 3 Clocks selled.
I introduced you to static elements, but we use only a field. Static members can be all kind of class elements. To show it, let's add a property, and keep private our selled field. In some part of the class add this code:
public static int Selled { get { return selled; } }And make private the selled field:
private int selled = 0;Finally, don't forget to change the last line ( we don't need to access the Clock.selled field, but the Clock.Selled property ).
Console.WriteLine("{0} Clocks selled.", Clock.Selled );Compile and runt it. If you get the same last message, you are on your way. Now you are able to use the static members.
I wanted to leave this, the static constructor. A constructor is a method called when creating a new instance class. There are differente kind of constructors, as viewed in a previous section. However, there still is one more constructor, the static one.
This constructor is called before any instance class is created in the current application. You can use it, for example, for initializing an DateTime object, or something like that. You must see the difference between initializing a field when the application is executed, and initializing one exactly before any instance class is created ( maybe no one in created, and the static constructor will never be called ).
Let's finish write the right application whe have been working on. We will be initializing the int selled field before any instance class is created. The you should change the field declaration to
private static int selled;Yes, no initialization. Also add the static constructor:
static Clock { selled = 0; }This way our programm look beter.
But be careful; for example, in the last modification, if you want to access to the Selled property before any instance class is created, you will receive an error message. Try to see when it is important to use it and when not.
By the way, if you exaclty modify the application as I told you, you should see the same last messages.