Static Initializers • A class can contain code in a static block that does not exist within a method body. • Static block code executes only once, when the class is loaded. • A static block is usually used to initialize static (class) attributes.
第一问:A里面的static段叫做 class A的类构造方法,叫class contructor。 第二问:对B没有影响。 补充:class constructor与instance constructor是不同的。 如下的例子里分别说明了class contrcutro和instance condtructor class A{ static {} public A() {}}
再补充: class contructor是在类第一次被ClassLoader加载时执行的。 instance contrcutor是在类第一次被实例化时被执行的。
• A class can contain code in a static block that does not
exist within a method body.
• Static block code executes only once, when the class is
loaded.
• A static block is usually used to initialize static (class)
attributes.
第二问:对B没有影响。
补充:class constructor与instance constructor是不同的。
如下的例子里分别说明了class contrcutro和instance condtructor
class A{
static {}
public A()
{}}
class contructor是在类第一次被ClassLoader加载时执行的。
instance contrcutor是在类第一次被实例化时被执行的。
thanks All!
especially to wswhp(greenland) and feiyuegaoshan(飞跃)!
I think answers from you two are more appropriate.
you all, really help!