考虑以下基本类:
public class ConstructorExample {
public ConstructorExample(){
System.out.println("Constructor called.");
}
public static void main(String[] args) {
ConstructorExample ce = new ConstructorExample();
}
}
Run Code Online (Sandbox Code Playgroud)
执行上面的代码时,"构造函数被调用".只打印一次.显然,在调用main方法时会显式调用构造函数.
但是,当JVM加载类并最初启动应用程序时,为什么不调用构造函数?
没有执行main()方法jvm不构造类的对象,这就是为什么main()方法是static
它正在执行
ConstructorExample ce = new ConstructorExample();
Run Code Online (Sandbox Code Playgroud)
确认以下评论
\\ConstructorExample ce = new ConstructorExample();
| 归档时间: |
|
| 查看次数: |
2372 次 |
| 最近记录: |