Topic: 请教一道题

  Print this page

1.请教一道题 Copy to clipboard
Posted by: markriver
Posted on: 2005-04-09 00:45

public class Derived {
static {
System.out.println("static code here");
}
public static void main(String[] args) {
Derived d=new Derived();
Derived d1=new Derived();
}
}
输出: static code here

public class Derived {
{
System.out.println("static code here");
}
public static void main(String[] args) {
Derived d=new Derived();
Derived d1=new Derived();
}
}
输出: static code here
static code here

解释: 为什么?尤其是第二段

2.Re:请教一道题 [Re: markriver] Copy to clipboard
Posted by: robinlet
Posted on: 2005-04-22 21:06

static {
System.out.println("static code here");
}
是类装载的时候进行初始化的。无论生成多少实例,都只执行1次。

{
System.out.println("static code here");
}
是初始化实例的,每次生成实例都会执行1次。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923