Home  >  Article  >  类库下载  >  The order of initialization of java objects

The order of initialization of java objects

高洛峰
高洛峰Original
2016-10-15 17:06:551954browse

Java code

public class Son extends Father {  
  
    String value = null;//2  
  
    public Son() {  
        super();  //1  
        System.out.println("Son:  " + value);//3  
    }  
  
    public static void main(final String[] args) {  
        new Son();  
    }  
}  
  
  
class Father {  
  
    public Father() {  
        if (this instanceof Son) {  
            Son lower = (Son) this;  
            lower.value = "test";  
        }  
    }  
}  
  
  
class Father {  
  
    public Father() {  
        if (this instanceof Son) {  
            Son lower = (Son) this;  
            lower.value = "test";  
        }  
    }  
}

Download

The result of this is null
Step 1 Set as test
Step 2 Set as null
Step 3 Print out null

If not String value = null ; Just String value; Download

Step 1 Set to test
Step 2 Do nothing, because there is already a value, no need to set it to the default null value
Step 3 Print out null

So there is a difference between not setting a value for a field and setting it to null.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn