《Java Language SpecificationSecond Edition》 8.7 Static Initializers Any static initializers declared in a class are executed when the class is initialized and, together with any field initializers (§8.3.2) for class variables, may be used to initialize the class variables of the class (§12.4). StaticInitializer: static Block It is a compile-time error for a static initializer to be able to complete abruptly (§14.1, §15.6) with a checked exception (§11.2). It is a compile-time error if a static initializer cannot complete normally (§14.20). The static initializers and class variable initializers are executed in textual order. Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope. See §8.3.2.3 for the precise rules governing forward reference to class variables. If a return statement (§14.16) appears anywhere within a static initializer, then a compile-time error occurs. If the keyword this (§15.8.3) or the keyword super (§15.11, §15.12) appears anywhere within a static initializer, then a compile-time error occurs
System.out.println("");被运行了!
你也喜欢麻辣教师吗,我很喜欢阿
比如说class.forName();
8.7 Static Initializers
Any static initializers declared in a class are executed when the class is initialized and, together with any field initializers (§8.3.2) for class variables, may be used to initialize the class variables of the class (§12.4).
StaticInitializer:
static Block
It is a compile-time error for a static initializer to be able to complete abruptly (§14.1, §15.6) with a checked exception (§11.2). It is a compile-time error if a static initializer cannot complete normally (§14.20).
The static initializers and class variable initializers are executed in textual order.
Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope. See §8.3.2.3 for the precise rules governing forward reference to class variables.
If a return statement (§14.16) appears anywhere within a static initializer, then a compile-time error occurs.
If the keyword this (§15.8.3) or the keyword super (§15.11, §15.12) appears anywhere within a static initializer, then a compile-time error occurs
static{
}块
那么是不是 不加static
就只有 调用构造函数时才会被运行啊
而加了static 在类被初始化的时候就运行了