码迷,mamicode.com
首页 > 编程语言 > 详细

java对象初始化的顺序

时间:2016-10-09 14:38:37      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:webservice   springmvc   rest   html5   websocket   

Java代码  

  1. public class Son extends Father {  

  2.   

  3.     String value = null;//2  

  4.   

  5.     public Son() {  

  6.         super();  //1  

  7.         System.out.println("Son:  " + value);//3  

  8.     }  

  9.   

  10.     public static void main(final String[] args) {  

  11.         new Son();  

  12.     }  

  13. }  

  14.   

  15.   

  16. class Father {  

  17.   

  18.     public Father() {  

  19.         if (this instanceof Son) {  

  20.             Son lower = (Son) this;  

  21.             lower.value = "test";  

  22.         }  

  23.     }  

  24. }  

  25.   

  26.   

  27. class Father {  

  28.   

  29.     public Father() {  

  30.         if (this instanceof Son) {  

  31.             Son lower = (Son) this;  

  32.             lower.value = "test";  

  33.         }  

  34.     }  

  35. }  


下载

这个的结果是 null 
步骤1  设置为test 
步骤2  设置为null 
步骤3 打印出来null 

如果 不是   String value = null ; 只是   String value; 下载

步骤1  设置为test 
步骤2  不做任何事情,因为已经有值了,不用设置为默认的null值了 
步骤3 打印出来null
 

所以  一个字段不设置值 和 设置为null  是有区别的。 


java对象初始化的顺序

标签:webservice   springmvc   rest   html5   websocket   

原文地址:http://12146875.blog.51cto.com/12136875/1859711

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!