O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

7.4. Static constructors (Java static initializers)

C# has two different kinds of constructors, static and instance – depending on whether they are declared with the static keyword or not. While C# instance constructors are similar to Java constructors, C#'s static constructors are similar to Java static initializers. Static constructors, like their instance counterparts, must have the same name and case as the class of which it is a member.

However, unlike instance constructors, static constructors:

  • cannot have parameters;

  • cannot have accessibility modifiers;

  • cannot be called explicitly.

Static constructors are automatically invoked before the first static class member is utilized. Here is an example of a static constructor.

 1: using System; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required