Pages

Saturday, March 12, 2011

Static Class


  1. Static Class cannot be instantiated unlike the unstatic class. You should directly access its Method via the ClassName.MethodName

  2. A Program can't tell when it is going to load static class but its definitely loaded before the call.

  3. A Static class will always have the static constructor and its called only once since after that its in the memory for its lifetime.

  4. A Static class can contain only static members. So all the members and functions have to be static.

  5. A Static class is always sealed since it cannot be inherited further. Further they cannot inherit form any other class (except Object)

Static Constructor

This is a special constructor and gets called before the first object is created of the class. The time of execution cannot be determined, but it is definitely before the first object creation - could be at the time of loading the assembly. Notes for Static Constructors:
  1. It is used to initialize static data members.
  2. Can't access anything but static members.
  3. The static constructor should be without parameters.
  4. Can't have access modifiers like Public, Private or Protected.
  5. There can be only one static constructor in the class.
  6. A class can have static as well as non static constructor without arg at same time.

The syntax of writing the static constructors is also damn simple. Here it is:

public class Sample{
    static Sample()
    {
        // Only static members are accessible.
        // Initialization code can be here.
    }
    // Other class methods goes here
}

Let us understand features step by step here.

Firstly, the call to the static method is made by the CLR and not by the object, so we do not need to have the access modifier to it.

Secondly, it is going to be called by CLR, who can pass the parameters to it, if required. So we cannot have parameterized static constructor.

Thirdly, non-static members in the class are specific to the object instance. So static constructor, if allowed to work on non-static members, will reflect the changes in all the object instances, which is impractical. So static constructor can access only static members of the class.

Fourthly, overloading needs the two methods to be different in terms of methods definition, which you cannot do with Static Constructors, so you can have at the most one static constructor in the class.

Now, one question raises here, can we have two constructors as:

public class Sample
{
    static Sample()
    {
        // Only static members are accessible.
        // Initialization code can be here.
    }
    public Sample()
    {
        // Code for the First Constructor.
    }

    // Other class methods goes here
}
This is perfectly valid, though doesn't seem to be in accordance with overloading concepts. But why? Because the time of execution of the two methods are different. One is at the time of loading the assembly and one is at the time of object creation.