17.11 Static constructors

原创 2006年05月21日 02:53:00
A static constructor is a member that implements the actions required to
initialize a class. Static constructors are
declared using static-constructor-declarations:
attributesopt static-constructor-modifiers identifier ( )
externopt static
static externopt
A static-constructor-declaration may include a set of attributes (§24) and
an extern modifier (§17.5.7).
Chapter 17 Classes
The identifier of a static-constructor-declaration must name the class in
which the static constructor is declared.
If any other name is specified, a compile-time error occurs.
When a static constructor declaration includes an extern modifier, the
static constructor is said to be an external
static constructor. Because an external static constructor declaration
provides no actual implementation, its
static-constructor-body consists of a semicolon. For all other static
constructor declarations, the staticconstructor-
body consists of a block, which specifies the statements to execute in
order to initialize the class. This
corresponds exactly to the method-body of a static method with a void
return type (§17.5.8).
Static constructors are not inherited, and cannot be called directly.
The static constructor for a class executes at most once in a given
application domain. 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 is created.
? Any of the static members of the class are referenced.
If a class contains the Main method (§10.1) in which execution begins, the
static constructor for that class
executes before the Main method is called. If a class contains any static
fields with initializers, those initializers
are executed in textual order immediately prior to executing the static
[Example: The example
using System;
class Test
static void Main() {
class A
static A() {
Console.WriteLine("Init A");
public static void F() {
class B
static B() {
Console.WriteLine("Init B");
public static void F() {
must produce the output:
Init A
Init B
because the execution of A’s static constructor is triggered by the call
to A.F, and the execution of B’s static
constructor is triggered by the call to B.F. end example]
It is possible to construct circular dependencies that allow static fields
with variable initializers to be observed in
their default value state.
[Example: The example
using System;
class A
public static int X;
static A() { X = B.Y + 1;}
class B
public static int Y = A.X + 1;
static B() {}
static void Main() {
Console.WriteLine("X = {0}, Y = {1}", A.X, B.Y);
produces the output
X = 1, Y = 2
To execute the Main method, the system first runs the initializer for B.Y,
prior to class B’s static constructor.
Y’s initializer causes A’s static constructor to be run because the value
of A.X is referenced. The static constructor
of A in turn proceeds to compute the value of X, and in doing so fetches
the default value of Y, which is zero. A.X
is thus initialized to 1. The process of running A’s static field
initializers and static constructor then completes,
returning to the calculation of the initial value of Y, the result of which
becomes 2. end example]

Item 1: Consider static factory methods instead of constructors

A class can provide a public static factory method to return a instance of itself.Advantage: When a ...
  • mtjwy
  • mtjwy
  • 2015年10月23日 13:54
  • 236

《Effective_Java》 Item1:Consider static actor methods instead of constructors

使用静态工厂的优势 1.  不同于构造函数,可以赋予静态工厂更有意义的名字         eg. BigInteger的构造方法BigInteger(int, int, Random)返回一个可...
  • llsit
  • llsit
  • 2011年10月12日 23:01
  • 357

Aspose.Words 11月新版17.11发布 | 修复表格在特定条件下丢失

Aspose.Words 11月新版17.11发布,修复表格在特定环境下丢失的问题。

C++对象模型(二):The Semantics of Copy Constructors(拷贝构造函数之编译背后的行为)

本文是 Inside The C++ Object Model's Chapter 2  的部分读书笔记。 有三种情况,需要拷贝构造函数: 1)object直接为另外一个object的初始值 2)ob...


构造方法 Constructors 什么是 一个方法, 用于创建对象 创建对象时会被调用 new 类名(); 格式 public class 类名 { public 类...

Constructors__<<C++ primer>>

""" Classes control object initialization by defining one or more special member functions known as...

C++拷贝构造函数语意学 copy constructor(The Semantics of Constructors)

使用copy constructor的三种情况有三种情况,会以一个object的内容作为另一个class object的初值。 1.显式的以一个object的内容作为另一个class object的...

基本语言细节--《深度探索C++对象模型》--(2)The Semantics of Constructors--总结点

主要是针对Lippmen 书中C++中构造函数以及复制构造函数的论述做一个大致总结,以期说明编译器在C++语言背后为我们做了哪些事情!...

C++构造函数语意学 - Constructor(The Semantics of Constructors)

1.默认构造函数(Default Constructor)default constructors 在编译器需要时被合成出来。被合成出来的constructor只执行编译器所需的行动。也就是说defa...
您举报文章:17.11 Static constructors