sudhir kumar
What is difference between Private and Static Constructor?
By sudhir kumar in OOP/OOD on Oct 17 2011
  • raja sekhar
    Dec, 2011 14


    Static Constructor

    1. Used to initialize the static members of a class.
    2. Can not access non-static members.
    3. Executes before the first instance of a class. We can not determine the time of execution.
    4. Executes by the CLR not by the object of a class.
    5. There are no parameterized static constructors since it is handled by the CLR not by the object.
    6. Time of execution might be at the loading of contained assembly.

    Private Constructor
    1. Used to restrict a class to be instantiated and to be inherited.
    2. Used whenever a class contains only static members.

    • 2
  • Rambabu CH
    Oct, 2011 20

    1)A static constructor is called before the first instance is created. i.e. global initializer.

    Whereas Private constructor is called after the instance of the class is created.

    2)Static constructor will be called first time when the class is referenced. Static constructor is used to initialize static members of the class.

      Static members will not be initialized either by private or public constructor.

    3)The static constructor will only be executed once.

      The private constructor will be executed each time it is called.

    • 2
  • shyam panwar
    Oct, 2011 19

    In my opinion, private are used with in the class only where it was declared and static methods can be used any where by using static member.

    • 1
  • sudhir kumar
    Oct, 2011 17

    Private Constructor - This is the constructor whose access modifier is private. private constructor is used to prevent a class to be instantiated. But, if a class has other public constructors, then that can be instantiated. A class can have multiple private constructor and can call it by another constructor.
    For example:-
    Class A
    {
          int a;
          private A()
          {             
           }
           public A(int b) : A()  // Calling private constructor by another constructor.
           {
                    this.a=b;
            }
    }


    Static Constructor :- Static constructor is used to initialize static members of a class. It is called by CLR, not by creating instance of the class. As it is called by CLR, it is not certain when it is called. But it is called when class is loaded. It can not be explicitly called by code. Static constructor has no any parameter. A class can have only one static constructor.

    • 1
  • sandeep kumar
    Jan, 2012 13

    Private Constructor - This is the constructor whose access modifier is private. private constructor is used to prevent a class to be instantiated. But, if a class has other public constructors, then that can be instantiated. A class can have multiple private constructor and can call it by another constructor.
    For example:-
    Class A
    {
          int a;
          private A()
          {             
           }
           public A(int b) : A()  // Calling private constructor by another constructor.
           {
                    this.a=b;
            }
    }


    Static Constructor :- Static constructor is used to initialize static members of a class. It is called by CLR, not by creating instance of the class. As it is called by CLR, it is not certain when it is called. But it is called when class is loaded. It can not be explicitly called by code. Static constructor has no any parameter. A class can have only one static constructor.

    • 0
  • sudhir kumar
    Oct, 2011 18

    Sorry to all, I made a mistake on my previous code in line 7. Please correct this.
    For example:-
    Class A
    {
          int a;
          private A()
          {             
           }
           public A(int b) : this()  // Calling private constructor by another constructor.
           {
                    this.a=b;
            }
    }

    • 0
  • raghavendra setty
    Oct, 2011 18

    Normally one uses the private constructor to create a singleton instance of a class, 

    Static constructor is called only once in during application lifetime, so its a good place to initialize any static members of the class.

    • 0
  • Uttam Chaturvedi
    Oct, 2011 17

    1. Static constructor is called before the first instance of class is created, wheras private constructor is called after the first instance of class is created.

    2. Static constructor will be executed only once, whereas private constructor is executed everytime, whenever it is called.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS