12

What is the real difference between a C# static constructor and a Java static block?

They both must be parameterless. They are both called only once, when the related class is first used.

Am I missing something, or are they the same thing, just with different names?

Mackenzie
  • 1,897
  • 1
  • 19
  • 25

4 Answers4

11

They are equivalent, except that a C# class can only have one static constructor (plus static field initializers).

Also, in C#, a static constructor will apply the beforefieldinit flag.

potatopeelings
  • 40,709
  • 7
  • 95
  • 119
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Obviously Java is not going to apply any beforefieldinit flags, as it isn't compiled to MSIL. – Joren Mar 17 '10 at 19:38
  • @Joren: I realize that. However, it is a difference in the behavior of the two features. – SLaks Mar 17 '10 at 19:39
  • 2
    Yes, but my (not so explicit, I admit) point was: It might be more useful to explain the difference in terms of the code semantics (i.e. order of field initialization and the static constructor) than in terms of implementation details. (Especially when it's details that don't even have any meaning for one of the two languages being considered.) – Joren Mar 17 '10 at 19:43
  • Would you both agree that if one was converting Java code to C#, that there would exist no risk of losing functionality? – Mackenzie Mar 17 '10 at 19:44
  • @Mackenzie: As long as the code doesn't have side effects that must be run lazily (or not), it should be equivalent. – SLaks Mar 17 '10 at 19:48
  • I thought java static blocks were executed when the class was loaded, not at the first use of the class (via a constructor or static method call). That's a subtle point since (iirc) classloaders will usually load all referenced classes at startup (so the static blocks will be executed immediately) but you can always load a class manually later. I have no idea how C# handles this. – bgiles Mar 17 '10 at 20:41
  • d'oh - a few hours after making that last comment I was skimming through "Effective Java, 2E" and came across that very issue. Java static blocks are only executed when the class is first used, not first loaded if they do any pre-loading. – bgiles Mar 19 '10 at 21:38
2

They look the same, the following example shows, that c# static constructor works the same as static block in java

protected Singleton()
{
    Console.WriteLine("Singleton constructor");
}

    private static readonly Singleton INSTANCE;

    static Singleton() {
        try {
           INSTANCE = new Singleton();
        }
        catch(Exception e) {
            throw new Exception();
        }
    }
Community
  • 1
  • 1
alexander
  • 29
  • 1
  • Swallowing an Exception just to raise a new empty one doesn't sound correct. Also, I don't see the Java equivalent to show that they are the same. – bracco23 Oct 09 '20 at 09:59
0

Yes They are equivalent Another point is java does not support static constructor but support static block and c# support static constructor.

Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
-2

They are not.

In C#, there blocks can only hold constructors. In java you are able to execute statements.

sheldon
  • 44
  • 2