The output from the following program is:
Non-Static
Static
Non-Static
Is this a compiler bug? I expected:
Static
Non-Static
Non-Static
because I thought the static constructor was ALWAYS called before the non-static constructor.
I tested this with Visual Studio 2010 using both .net 3.5 and .net 4.0.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StaticConstructorBug
{
class Program
{
static void Main(string[] args)
{
var mc = new MyClass();
Console.ReadKey();
}
}
public class MyClass
{
public MyClass()
{
Console.WriteLine("Non-static");
}
static MyClass()
{
Console.WriteLine("Static");
}
public static MyClass aVar = new MyClass();
}
}
Also, you can have a static constructor in a static class or a non-static class. A static constructor is used to initialize the static members of a class. The static constructor of a class is invoked the first time a static member of the class is accessed.
The constructors in Java can not be static because if the constructors are marked as static, they can not be called from the child class; thus, the child class's object will not be created. The program will not be compiled and throw a compile-time error.
You cannot call static methods using an object of the non-static class. The static methods can only call other static methods and access static members.
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.
See ECMA 334 §17.4.5.1:
17.4.5.1 Static field initialization
The static field variable initializers of a class declaration 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 (§17.11) 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
Specifically: "execution of the static field initializers occurs immediately prior to executing that static constructor".
Your static MyClass aVar
must be initialized before your static constructor executes (or, at least, it must appear that way). Without that static member, the static constructor should be called before any non-static constructors.
If you still want a MyClass
singleton, you can put it in a container class and refer to it using that, e.g.:
public static class MyClassSingleton
{
public static MyClass aVar = new MyClass();
}
It is caused by line public static MyClass aVar = new MyClass();
.
In fact the aVar = new MyClass();
is prepend to the static contrstructor. So your static constructor:
static MyClass() {
Console.WriteLine("Static");
}
is changed to:
static MyClass() {
aVar = new MyClass(); // this will run instance contstructor and prints "Non-Static"
Console.WriteLine("Static");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With