JAVA基础

时光清浅 2013-12-19 05:05:02
public class Reluctant {

private Reluctant instance = new Reluctant();

public Reluctant() throws Exception {
throw new Exception("-->Reluctant Exception");
}

public static void main(String[] args) {
try {
Reluctant r = new Reluctant(); //调用构造方法之后,为啥初始化instance?
System.out.println("-->main");
} catch (Exception e) {
System.out.println("-->main Exception");
}
}
}


我DEBUG了下,发现在构造方法和instance对象初始化之间,是死循环。
1.对象的初始化顺序?
2.为什么这里产生了递归调用?

请大家讲下,谢谢!
...全文
148 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
JessonLv 2013-12-20
  • 打赏
  • 举报
回复
引用 3 楼 huxiweng 的回复:
死循环构造。 你main里面new一个之后,初始化r的时候,r对象里面的成员instance又要初始化,instance new的时候又有成员instance..无限循环
说的对!
姜小白- 2013-12-20
  • 打赏
  • 举报
回复
引用 8 楼 fatg1988 的回复:
javap -c xx.class
灰常感谢
七神之光 2013-12-20
  • 打赏
  • 举报
回复
引用 5 楼 magi1201 的回复:
[quote=引用 2 楼 fatg1988 的回复:] Compiled from "TRRRR.java" public class test.TRRRR extends java.lang.Object{ public test.TRRRR() throws java.lang.Exception; Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class test/TRRRR 8: dup 9: invokespecial #3; //Method "<init>":()V 12: putfield #4; //Field instance:Ltest/TRRRR; 15: new #5; //class java/lang/Exception 18: dup 19: ldc #6; //String -->Reluctant Exception 21: invokespecial #7; //Method java/lang/Exception."<init>":(Ljava/lang/St ring;)V 24: athrow 循环引用构造 看标号5
大牛,这个怎么出来的,求方法技巧,想学习下[/quote]javap -c xx.class
qianfu111 2013-12-19
  • 打赏
  • 举报
回复
没见这样玩过呀,3楼讲的不错,楼主这是要想实现单例类吗?
___________小P 2013-12-19
  • 打赏
  • 举报
回复
我就过来看看技术
姜小白- 2013-12-19
  • 打赏
  • 举报
回复
引用 2 楼 fatg1988 的回复:
Compiled from "TRRRR.java" public class test.TRRRR extends java.lang.Object{ public test.TRRRR() throws java.lang.Exception; Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class test/TRRRR 8: dup 9: invokespecial #3; //Method "<init>":()V 12: putfield #4; //Field instance:Ltest/TRRRR; 15: new #5; //class java/lang/Exception 18: dup 19: ldc #6; //String -->Reluctant Exception 21: invokespecial #7; //Method java/lang/Exception."<init>":(Ljava/lang/St ring;)V 24: athrow 循环引用构造 看标号5
大牛,这个怎么出来的,求方法技巧,想学习下
姜小白- 2013-12-19
  • 打赏
  • 举报
回复
private Reluctant instance = new Reluctant(); instance成为Reluctant类的一个属性,构造Reluctant 也会再调用new Reluctant()
teemai 2013-12-19
  • 打赏
  • 举报
回复
死循环构造。 你main里面new一个之后,初始化r的时候,r对象里面的成员instance又要初始化,instance new的时候又有成员instance..无限循环
七神之光 2013-12-19
  • 打赏
  • 举报
回复
Compiled from "TRRRR.java" public class test.TRRRR extends java.lang.Object{ public test.TRRRR() throws java.lang.Exception; Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class test/TRRRR 8: dup 9: invokespecial #3; //Method "<init>":()V 12: putfield #4; //Field instance:Ltest/TRRRR; 15: new #5; //class java/lang/Exception 18: dup 19: ldc #6; //String -->Reluctant Exception 21: invokespecial #7; //Method java/lang/Exception."<init>":(Ljava/lang/St ring;)V 24: athrow 循环引用构造 看标号5
亲努力啊 2013-12-19
  • 打赏
  • 举报
回复
Reluctant r = new Reluctant(); r这个对象定义了instance 变量 并对其进行了初使化

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧