Constructors can not be static ?

Showing Answers 1 - 38 of 38 Answers

Prabir Sarkar

  • Jan 3rd, 2006
 

True

  Was this answer useful?  Yes

hari charagundla

  • Jan 4th, 2006
 

I think Answer is True.A class can have static constructor, the static constructor would only be executed when the first time the class is instanciated. not with every time a instance is created, a pulic constructor would be executed every time when the class is instantiated including the first time when static constructor is instanciated.Hari

Pinal Soni

  • Jan 9th, 2006
 

True Constructor can NOT be static.

Pan

  • Jan 31st, 2006
 

False, constructor can be declared static.

C# supports two types of constructor, a class constructor (static constructor) and an instance constructor (non-static constructor).

Static constructor is used to initialize static data members as soon as the class is referenced 
first time, whereas an instance constructor is used to create an instance of that class with 
<new> keyword. A static constructor does not take access modifiers or have parameters and can't access any non-static data member of a class.

Since static constructor is a class constructor, they are guaranteed to be called as soon as we refer to that class or by creating an instance of that class.

Ravinder Nain

  • Jan 31st, 2006
 

FalseAuthor trying to make us fool.This is a puzzling question.Author wants to know our presence of mind.As Mr. Naveen Said that author question is "Constructors can not be static" but constructors can be static.So ans is False

Ravi

  • Mar 28th, 2006
 

Answer is Fasle.

You can have static constructor in a class very well..By having static constructor it gets invoked before any instance variables declared in a class

  Was this answer useful?  Yes

Tommie

  • Mar 29th, 2006
 

* constructors can be static

* a static constructor must be parameterless

* static constructors will be executed before other constructors

* constructors can have access modifiers (but static constructors CAN NOT)

/*Sample*/

using System;

namespace ConsoleApplication2

{

      class Class1

      {

            static Class1()

            {

                  Console.WriteLine("static Class1()");

            }

            public Class1()

            {

                  Console.WriteLine("Class1()");

            }

            private Class1(string abc)

            {

                  Console.WriteLine("private Class1(string abc)");

            }

            [STAThread]

            static void Main(string[] args)

            {

                  //***** Return static Class1, then Class1

                  //Class1 myclass = new Class1();

                  //Console.ReadLine();

                  //***** Return static Class1, then private Class1

                  Class1 myclass = new Class1("test");

                  Console.ReadLine();

            }

      }

}

  Was this answer useful?  Yes

Guest

  • Apr 20th, 2006
 

B is the correct answer. We have the concept of static constructor. A static constructor

* is parameterless constructor.

* has no access modifier associated with it. it would be a compile time error otherwise.

* is executed before the class (that have static constructor) object is instantiated and executed only once i.e. at the time of first object instatiation.

* can not be accessed directly as instance constructors.

* can access only static members of the class.

There can be only one static constructor in the class, as static constructor is declared as 

public class ClassA

{

static ClassA() // simple signature--no access modifier/no parameter

{

}

}

so there is no way to declare another static constructor. (Concept of Method Overloading)

Moreover, Static constructor is called by the CLR(the runtime-execution environment of .NET), and not by the object, so there is no need of access modifier and parameters to static constructors.

amit sharma

  • Jul 17th, 2006
 

hi

Contructor can be static and it can be private also

  Was this answer useful?  Yes

skumarcode

  • Aug 31st, 2006
 

access modifiers are not allowed on static constructors

  Was this answer useful?  Yes

Answer is             False

We have a Static constructors......

 It is a special type of constructor, introduced with C#. It gets called before the creation of the first object of a class(probably at the time of loading an assembly). 

Example:

public class SomeClass()
{
  static SomeClass()
  {
     //Static members may be accessed from here
     //Code for Initialization
   }
}

  Was this answer useful?  Yes

dirkk

  • Jul 2nd, 2010
 

False:

One novel feature of C# is that it is also possible to write a static no - parameter constructor for a class.

Such a constructor will be executed only once, as opposed to the constructors written so far, which are instance constructors that are executed whenever an object of that class is created.

One reason for writing a static constructor is if your class has some static fields or properties that need to  be initialized from an external source before the class is first used.

  Was this answer useful?  Yes

Constructors can be static. 

Static constructors are used to initialize static members. Static constructors has following properties:
1) It can not have access modifier and parameters
2) It get executed when a class is instantiated or referenced static member is called.

  Was this answer useful?  Yes

constructor can be static.static constructors are used to initialized static datafields.static constructors doesn't have return type.static constructor can be invoked when first object creation of the class from the second object onwards it doesn't call the static constructor.

  Was this answer useful?  Yes

It is false.

If it is static, it can only constructed one time. See the following example.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1

{


class Program

{

class FOO

{

static int s = 0;

int ns = 0;

static FOO()

{

s=s+1;

Console.WriteLine("Static {0}",s);

}

public FOO()

{

s = s + 1;

Console.WriteLine("Public {0}",s);

}

private FOO(string a)

{

Console.WriteLine("Private", a);

}

void add(int i)

{

}

}

static void Main(string[] args)

{

FOO aFoo1 = new FOO();FOO aFoo2 = new FOO();

}

}

}



The output is ---

Static 1
Public 2
Public 3
Press any key to continue . . .

  Was this answer useful?  Yes

desireadil

  • Jul 28th, 2011
 

In C# . Net Constructors can be static. There are five constructor in C# . Net.
1. Default constructors or Non-parameterized constructor
2. Parametrize constructor
3. Static constructor
4. Private constructor
5. Copy constructor.
Static constructor is the only constructor which doesn't construct the instance type.

  Was this answer useful?  Yes

The answer is False.

Constructors CAN BE declared as static . Another thing to keep in mind is that you cannot use access modifiers with static constructors as you can do with static methods.

For Ex :

public static Class1() // INVALID STATEMENT
{

}

public static string getString() // VALID STATEMENT
{
return "";
}

  Was this answer useful?  Yes

Rajeeva

  • Sep 4th, 2011
 

Constructors can be static.
Static constructors are used to initialize static members. Static constructors has following properties:
1) It can not have access modifier and parameters
2) It get executed when a class is instantiated or referenced static member is called.

  Was this answer useful?  Yes

It's "False" , static constructors are used to initialize the static variables of a class.....

Code
  1. //Example

  2. public class Class1

  3. {

  4.        static int number1;

  5.        int number2;

  6.        

  7.        static Class1()

  8.       {

  9.          number1=10;

  10.        }

  11. }

  12.  

  Was this answer useful?  Yes

Yasmeen

  • Sep 23rd, 2011
 

False.
There are three constructors .Static,Private and Public Constructors.

  Was this answer useful?  Yes

False, constructor can be declared static.

C# supports two types of constructor, a class constructor (static constructor) and an instance constructor (non-static constructor).

but if constructor is static then they can access only static members

  Was this answer useful?  Yes

Aashish Chachra

  • Mar 2nd, 2012
 

A class can have only one static constructor.
Static constructor can not have any parameter.
Static constructor cannot have any access specifier, not even private.
It is used to initialize the static data members of the class.
For any number of object creation, the static constructor gets executed only once.

The static constructor gets executed when the class is used or referenced for the very first time in the application.

Static constructor can not be invoked by the programmer explicitly.

  Was this answer useful?  Yes

Sivavt

  • Mar 25th, 2012
 

Yes, there can be a static constructor. It will be at class level & executed just once. It is totally in run-times control & not sure of when this will be executed. But, it is assured that it will be executed before creating instance of the class.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions