GeekInterview.com
Series: Subject: Topic:
Question: 184 of 460

Constructors can not be static ?

Asked by: Interview Candidate | Asked on: Jan 1st, 2006
Showing Answers 1 - 38 of 38 Answers
Prabir Sarkar

Answered On : Jan 3rd, 2006

True

  
Login to rate this answer.
hari charagundla

Answered On : 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

Yes  2 Users have rated as useful.
  
Login to rate this answer.
naveentej

Answered On : Jan 8th, 2006

View all answers by naveentej

The Ans is False. Guys, the author statement says "We can't have static constructors" which is false becoz we can have static constructors.

Yes  2 Users have rated as useful.
  
Login to rate this answer.
Pinal Soni

Answered On : Jan 9th, 2006

True Constructor can NOT be static.

Yes  1 User has rated as useful.
  
Login to rate this answer.
imqwer

Answered On : Jan 24th, 2006

View all answers by imqwer

True, constructor cannot be static.

Yes  1 User has rated as useful.
  
Login to rate this answer.
Pan

Answered On : 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 
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.

Yes  1 User has rated as useful.
  
Login to rate this answer.
Ravinder Nain

Answered On : 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

Yes  1 User has rated as useful.
  
Login to rate this answer.
bulldog

Answered On : Mar 5th, 2006

View all answers by bulldog

False, for sure, constructors can be declare static

Yes  3 Users have rated as useful.
  
Login to rate this answer.
Ravi

Answered On : 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

  
Login to rate this answer.
Tommie

Answered On : 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();

            }

      }

}

  
Login to rate this answer.
Guest

Answered On : 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.

Yes  3 Users have rated as useful.
  
Login to rate this answer.
amit sharma

Answered On : Jul 17th, 2006

hi

Contructor can be static and it can be private also

  
Login to rate this answer.
skumarcode

Answered On : Aug 31st, 2006

access modifiers are not allowed on static constructors

  
Login to rate this answer.
pankajbanga

Answered On : Feb 26th, 2007

View all answers by pankajbanga

constructors can be static. this will clarify doubts about static constructors

http://www.c-sharpcorner.com/UploadFile/cupadhyay/StaticConstructors11092005061428AM/StaticConstructors.aspx

 

  
Login to rate this answer.

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
   }
}

  
Login to rate this answer.
anil7181

Answered On : May 15th, 2008

View all answers by anil7181

False.
Constructors can be static only for static members of class

  
Login to rate this answer.
dirkk

Answered On : Jul 2nd, 2010

View all answers by dirkk

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.

  
Login to rate this answer.
Sarika Macha

Answered On : Apr 30th, 2011

View all answers by Sarika Macha

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.

  
Login to rate this answer.
Mojian

Answered On : May 18th, 2011

View all answers by Mojian

Constructors can be static

  
Login to rate this answer.

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.

  
Login to rate this answer.

false

  
Login to rate this answer.
Seeker911

Answered On : Jun 14th, 2011

View all answers by Seeker911

False. Constructors can be declared as Static.

  
Login to rate this answer.
Tanveer Ahmad

Answered On : Jul 6th, 2011

View all answers by Tanveer Ahmad

There is static cunstructor for static class

  
Login to rate this answer.
twborn88888

Answered On : Jul 7th, 2011

View all answers by twborn88888

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 . . .

  
Login to rate this answer.
desireadil

Answered On : Jul 28th, 2011

View all answers by desireadil

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.

  
Login to rate this answer.

In c# static constructor available.

if we have different constructor in the class. static constructor execute first , after that only other constructor will execute

  
Login to rate this answer.
vishnupavadi

Answered On : Aug 8th, 2011

View all answers by vishnupavadi

Its possible in C#. Tommy has answered perfectly...

  
Login to rate this answer.
Dinesh M Yadav

Answered On : Aug 28th, 2011

View all answers by Dinesh M Yadav

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 "";
}

  
Login to rate this answer.
Rajeeva

Answered On : Sep 4th, 2011

View all answers by Rajeeva

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.

  
Login to rate this answer.
Prakash Jangir

Answered On : Sep 23rd, 2011

View all answers by Prakash Jangir

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.  

  
Login to rate this answer.
Yasmeen

Answered On : Sep 23rd, 2011

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

  
Login to rate this answer.
wickerss4

Answered On : Oct 7th, 2011

View all answers by wickerss4

No, Constructors can be Static...
http://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.80).aspx

  
Login to rate this answer.
waynelinner

Answered On : Oct 13th, 2011

View all answers by waynelinner

Incorrect

  
Login to rate this answer.
HansSr

Answered On : Oct 26th, 2011

View all answers by HansSr

true

  
Login to rate this answer.

Constructors can be static in c#

  
Login to rate this answer.

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

  
Login to rate this answer.
Aashish Chachra

Answered On : 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.

  
Login to rate this answer.
Sivavt

Answered On : Mar 25th, 2012

View all answers by Sivavt

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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.