ARTICLE

Constructor vs Static Constructor

Posted by Amit Saxena Articles | Visual C# October 23, 2008
This article explains you the basic difference between the constructor and static constructor via example.
Reader Level:

A Constructor is usually used to initialize data. However Static Constructor is used to initialize only static members. Here I am just talking about the Constructors. How they get initialized and how they behave.

Things to know about Static Constructor

  1. It is used to initialize static data members.

  2. Can't access anything but static members.

  3. Can't have parameters

  4. Can't have access modifiers like Public, Private or Protected.

Now once you understand the above points, you can appreciate the difference between Static Class and Unstatic 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)

Let's look at a normal Constructor

class
Program
{
    static void Main(string[] args)
      { 

         /* Note two things

        1.Here you get to instantiate the Constructor in the code wherever you like.

        2.If you debug you get to goto the Constructor and see what is being done.

         */
         MyExample myExample = new MyExample();
         myExample.Print();
      }
}

public class MyExample
{
    private static int StaticVariable ;
    public MyExample()
       {
             if (StaticVariable < 10)
             {
                    StaticVariable = 20;
             }
            else
             {
                   StaticVariable = 100;
             }
       }
  

    public void Print()
    {
        Console.WriteLine(StaticVariable);
    }
}

Now consider this second example for static class

class Program
{
    static void Main(string[] args)
    {

       /* Note the following

            1.You dont get to instantiate for sure so you dont know when the constructor was called.

            2.Secondly you can access your method directly.

       */

      //MyExampleStatic myExampleStatic = new MyExampleStatic();
       MyExampleStatic.Print();
}
}

static class MyExampleStatic
{
    private static int StaticVariable;
    static MyExampleStatic()
   {
        if (StaticVariable < 10)
          {
          StaticVariable = 20;
          }
    else
     {
          StaticVariable = 100;
      }
  }
    public static void Print()
   {
        Console.WriteLine(StaticVariable);
    } 
}

 

The point is that static member could be used only by a static Constructor or static Function. Hope you have a Static Constructing life.

Login to add your contents and source code to this article
post comment
     

I got 20 for the both instance constructor and static constructor

Posted by Alavudeen Abdulsukkoor Jul 09, 2010

Though the difference between static class and nonstatic class in the beginning does cause some confusion for a reader who expects to learn only static constructor, still on the whole the article is ok, good.

Posted by Mamta M Nov 05, 2008

It's very interesting point but, I think you should give a real live case when the static class or the static constructor is only used like the singleton for example

Posted by Bechir Bejaoui Oct 27, 2008
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.