Passing “this” in java constructor

后端 未结 9 722
梦毁少年i
梦毁少年i 2020-11-30 11:39

Look into the following code:

public class ClassA {
    private boolean ClassAattr = false;

    public ClassA() {    
        ClassAHandler handler = new Cl         


        
9条回答
  •  眼角桃花
    2020-11-30 11:54

    This page has a very good explanation of why letting the "this" reference escape is a bad idea:

    http://www.ibm.com/developerworks/java/library/j-jtp0618.html#2

    Check the "Don't publish the "this" reference during construction" section

提交回复
热议问题