1

Recently I have been asked a question in interview.

Interviewer asked the below question

-When and who creates the instance of static class? Clearly creating a instance means allocating space in the memory. We cannot call methods without allocating space in memory.

I was confused because as per my knowledge static classes don't get instances created So I searched on internet and I found that for static class only once instance is created and that is when the first static method is called for the first time. Is it correct?

Second question who creates the instance? Is it CLR?

Shahzad Ahamad
  • 809
  • 1
  • 11
  • 30
  • then what is your answer at your interview? – toha Jun 15 '17 at 03:15
  • 1
    I believe static classes are not instantiated. – zerkms Jun 15 '17 at 03:16
  • 2
    There is no instance for a static class. – RJM Jun 15 '17 at 03:16
  • I believe static class only once instance, as you know – toha Jun 15 '17 at 03:20
  • 3
    @toha what you posted is misleading (and probably incorrect). – zerkms Jun 15 '17 at 03:20
  • 3
    Documentation provides immediate answer - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members... It may be good idea to clarify what exactly you don't understand there. – Alexei Levenkov Jun 15 '17 at 03:22
  • https://www.dotnetperls.com/static at the sample: static class Perls what I think is, the class has one instance even we do not create any instance. But it is exist on memory. I think any thing create the instance by default But the site says: First example. Static denotes things that are singular. They are part of no instance. Static often improves performance, but makes programs less flexible. – toha Jun 15 '17 at 03:25
  • @toha memory required for a type to exist in runtime not necessary belongs to an **instance**. "I think any thing create the instance by default" --- that's surely not true. – zerkms Jun 15 '17 at 03:27
  • 1
    I believe you are confusing _static classes_ with **singletons**. Otherwise to take your title as gospel would be a trick question by the employer –  Jun 15 '17 at 03:35
  • @community, would this qualify as a duplicate [How exactly do static fields work internally?](https://stackoverflow.com/q/14781993/251311) – zerkms Jun 15 '17 at 03:35
  • "A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated." Now, that's the first sentence in the Microsoft documentation that Alexei posted a link to. What does it take to convince people, if that doesn't convince? – Bent Tranberg Jun 15 '17 at 04:02
  • @AlexeiLevenkov Thankyou, It cleared my concepts – Shahzad Ahamad Jul 19 '17 at 02:44
  • @RJM can you please +1 the question I have corrected the question I am unable to post questions now – Shahzad Ahamad Aug 01 '17 at 03:21
  • @simba - I was not the person who downvoted the question. Also, the question is still wrong. The statement, `I found that for static class only once instance is created`, is incorrect. A static class does not have any instance. – RJM Aug 01 '17 at 22:29
  • @RJMThe statement you mentioned is actually my question I read it somewhere and wanted to confirm if the statement is correct or not, look carefully I have asked "is it correct?" – Shahzad Ahamad Aug 02 '17 at 02:13

2 Answers2

7

Static "instances" actually get "instantiated" (that is, their static constructor gets invoked) the first time code references the static class:

void Main()
{
    Console.WriteLine("Not instantiated yet!");
    Stat.A();
}

// Define other methods and classes here

static class Stat
{
    static Stat()
    {
        Console.WriteLine("Instantiated!");     
    }

    public static void A()
    {
        Console.WriteLine("A was called!");
    }
}

Prints the following:

Not instantiated yet!
Instantiated!
A was called!

As mentioned elsewhere, this isn't technically instantiation, but it's pretty close; you just have to think of it as being that the instance exists "elsewhere" and the runtime manages it for you.

Clint
  • 6,133
  • 2
  • 27
  • 48
  • 2
    That's a misuse of the term `instance`. Calling a static constructor does not constitute an instantiation. – RJM Jun 15 '17 at 03:30
  • 3
    It would be ideal to not name them "instances" even in quotes. It is just a static class constructor invoked and fields initialised. https://stackoverflow.com/a/15177713/251311 – zerkms Jun 15 '17 at 03:31
  • @zerkms Agreed, but conceptually there's really not that much of a gap, I think there's a risk of over-complication of the mental model when thinking in terms of instances vs non-instances, it helps to just treat a static class for all intents and purposes as a singular, global, typename-referenced instance. (at least it does for me) – Clint Jun 15 '17 at 03:32
  • 1
    Indeed, it depends how precise one wants to be. For me it's a part of a type structure, and I would avoid as much as possible to not name it "instance". It's just a preference though. – zerkms Jun 15 '17 at 03:34
  • @zerkms wherever I've worked we've always just referred to them as "static instances" or "the static class" - another example of one of the lovely linguistic ambiguities in software development. – Clint Jun 15 '17 at 03:35
  • @Clint and who does it? Is it CLR? – Shahzad Ahamad Jun 15 '17 at 03:39
  • 1
    _"....Static "instances" actually get 'instantiated'..."_ - Incorrect. _"[A static class is basically the same as a non-static class, but there is one difference: a static class **cannot be instantiated**](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members)"_ –  Jun 15 '17 at 03:40
  • @clint Can you please provide some references to support your answers specially for the CLR part? – Shahzad Ahamad Jun 15 '17 at 03:46
  • @simba I can't, but because it's a .NET feature, the logical conclusion is that the CLR is managing it. – Clint Jun 15 '17 at 03:47
  • @RJM you are right, I was confused and have corrected my question. – Shahzad Ahamad Jul 19 '17 at 02:47
1

A static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

The following list provides the main features of a static class:

  • Contains only static members.
  • Cannot be instantiated.
  • Is sealed.
  • Cannot contain Instance Constructors.
Nguyen Huynh
  • 523
  • 4
  • 6
  • But it can contain static constructors, which is what I think the question is referring to. https://dotnetfiddle.net/vFXDoe – Andrew Shepherd Jun 15 '17 at 03:23
  • 1
    The question doesn't say anything about static constructors, but does explicitly ask about "creating an instance" multiple times. – RJM Jun 15 '17 at 03:25
  • Agree it can contain static constructors, but you cannot use new keyword to create instance. – Nguyen Huynh Jun 15 '17 at 03:33