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

The order of initialization of java objects

高洛峰
Release: 2016-10-15 17:06:55
Original
1994 people have browsed it

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";  
        }  
    }  
}
Copy after login

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.


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template