java对象初始化的顺序
文章目录
Java代码
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";
}
}
}
下载
这个的结果是 null
步骤1 设置为test
步骤2 设置为null
步骤3 打印出来null
如果 不是 String value = null ; 只是 String value; 下载
步骤1 设置为test
步骤2 不做任何事情,因为已经有值了,不用设置为默认的null值了
步骤3 打印出来null
所以 一个字段不设置值 和 设置为null 是有区别的。
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册⭐️ Admin快速开发: https://show.cool-admin.com/
赞 0⭐️ 官方文档: https://cool-js.com
赞 0⭐️ chatgpt在线体验: https://cool-js.com/ai/chat-vip/index.html?r=helloworld
赞 0