Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do static fields come into existence?

Tags:

c#

.net

I'm trying to understand when exactly static fields come into existence and have been reading this MSDN article - http://msdn.microsoft.com/en-us/library/79b3xss3 - but it seems to contradict itself:

First it says:

Static members are initialized before the static member is accessed for the first time and before the static constructor, if there is one, is called.

But then it goes on to say:

If your class contains static fields, provide a static constructor that initializes them when the class is loaded.

So, my question is basically this: When are static fields actually initialized and when do they first come into existence? Is it before the static constructor is called, during, or after?

Many thanks!

like image 789
Dr Mojo Avatar asked Feb 14 '11 18:02

Dr Mojo


People also ask

What does it mean for a field to be static?

The term static refers to a situation where the fields do not vary with time. Static electric and magnetic fields are two distinct phenomena, both characterized by steady direction, flow rate and strength (thus a frequency of 0 Hz).

When should a field be static Java?

In Java, when we declare a field static, exactly a single copy of that field is created and shared among all instances of that class. It doesn't matter how many times we instantiate a class. There will always be only one copy of static field belonging to it.

What does a static field mean in Java?

Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory.


2 Answers

The C# Language specification states (in 10.5.5.1 Static field initialization):

The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration. If a static constructor (§10.12) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class

I believe the guidance in your second line is actually to initialize static fields that are not done inline, using field initializers. In that case, you should use a static constructor to guarantee that they will be initialized prior to usage.

As you can see from the specification, the actual "time" they come into existence is implementation specific, and subject to change. The only guarantee is that they will exist prior to the static constructor being called, which will always occur prior to their usage in code. This specification states (in 10.12):

The execution of a static constructor is triggered by the first of the following events to occur within an application domain:

· An instance of the class type is created.

· Any of the static members of the class type are referenced.

like image 96
Reed Copsey Avatar answered Oct 01 '22 19:10

Reed Copsey


It's like "normal" fields. So before the static constructor. You can check with a very simple program.

The order is:

  • static fields
  • static constructor
  • "normal" fields
  • "normal" constructor

A sample program:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Start");
        int b = A.B;
        Console.WriteLine("Read A.B");
        new A();
        Console.WriteLine("Built A");
    }
}

class A
{
    public static int B = mystaticfunc();

    static int mystaticfunc() {
        Console.WriteLine("Static field");
        return 1;
    }

    static A()
    {
        Console.WriteLine("Static constructor");
    }

    int C = myfunc();

    static int myfunc()
    {
        Console.WriteLine("Field");
        return 1;
    }

    public A()
    {
        Console.WriteLine("Constructor");
    }
}

Output:

Start
Static field
Static constructor
Read A.B
Field
Constructor
Built A
like image 40
xanatos Avatar answered Oct 01 '22 21:10

xanatos