package com.java.essence_36; import java.util.ArrayList; import java.util.List; /** * Created by lw on 14-5-23. * <p/> * 构造器做了什么? * 对象是谁创建的?构造器? * this究竟是什么? */ class SupperConstructorInvocation { SupperConstructorInvocation() { this(1);//仅仅能是第一句调用其它构造器,默认super();最后递归到Object //this(1,2); //error //super(); //error } SupperConstructorInvocation(double d) { //this(); System.out.println(d); } SupperConstructorInvocation(int i, int y) { //this(); } } public class ConstructorInvocation extends SupperConstructorInvocation { public void demo() { //this(); //error ,仅仅能在构造器中调用构造器 } public static void main(String[] args) { /*ConstructorInvocation constructorInvocation =new ConstructorInvocation(1);*/ //构造器不能继承 //測试。new对象时候构造器的參数列表运行了吗? new SupperConstructorInvocation(100 / Math.PI); //输出31.830988618379067,说明100 / Math.PI运行了才运行构造函数里的内容 /** * 运行内存 * -Xms1m -Xmx1m * 运行结果如图1,说明对象创建成功后才去运行构造方法 * 不是构造方法创建的对象 */ CreateObject.getMaxObjects(); } } class CreateObject { CreateObject() { CreateObject object = new CreateObject(); } CreateObject(int temp) { System.out.println("CreateObject(int temp)->run..."); } private static final List<CreateObject> CREATE_OBJECT_LIST = new ArrayList<>(); public static void getMaxObjects() { int temp = 0; while (true) { try { CREATE_OBJECT_LIST.add(new CreateObject(temp = 1)); temp = 0; } catch (Exception e) { } finally { System.out.println("对象创建时成功时候:构造方法运行了吗?" + (temp == 0)); } } } }
CreateObject.getMaxObjects();执行结果
javac -p CreateObject.class反编译查看
CreateObject() { CreateObject object = new CreateObject(); }反编译内容
依据#2所指向的常量池地址创建一个新对象,
class This { This() { } This(int i) { } /** * 相当于 This(This this){ } This(This this,int i){ } */ }对于例如以下代码
This t = new This();
This t = new This(r);而不是 t 。由于此时对象还没有初始化,仅仅有对象全然创建后,才会将引用返回并赋值给 t
原文:http://www.cnblogs.com/gcczhongduan/p/5138656.html