java 设置date为null_谁动了我的奶酪?--java实例初始化的顺序问题

故事背景

有一天,老鼠小白发现了一个奇怪的问题,它的奶酪的生产日期被谁搞丢了,不知道奶酪是否过期,可怎么吃呀?

b0bdce60188fa57345b4cbe570d973d7.png

让我们来看看吧

import java.util.Date;public class Cheese {public static final Cheese cheese=new Cheese();private final long produceTimes;private static final long produceDate =new Date(119,8,1).getTime();private Cheese() {produceTimes=new Date().getTime()-produceDate;}public long produceTimes() {return produceTimes;}public static void main(String[] args) {System.out.println("current time in day(from 1900:00:00) : "+new Date().getTime()/(1000*60*60*24L));System.out.println("cheese had produces : "+ cheese.produceTimes()/(1000*60*60*24L) +" days");}}

按照小白的预期,程序该跑出奶酪还剩下多少时间(秒),可是打印出的结果确实奶酪不会过期

current time in day(from 1900:00:00) : 18153cheese had produces : 18153 days

这是怎么回事呢?

074c2c28095891d7f731f61cac8065bf.png

查看代码提交记录,发现老鼠小蓝有修改记录,仅仅调整了两行程序的顺序,小白原来的代码如下:

import java.util.Date;public class Cheese {private final long produceTimes;private static final long produceDate =new Date(119,8,1).getTime();public static final Cheese cheese=new Cheese();private Cheese() {produceTimes=new Date().getTime()-produceDate;}public long produceTimes() {return produceTimes;}public static void main(String[] args) {System.out.println("current time in day(from 1900:00:00) : "+new Date().getTime()/(1000*60*60*24L));System.out.println("cheese had produces : "+ cheese.produceTimes()/(1000*60*60*24L) +" days");}}

仅仅修改了两个变量的顺序,输出的结果就大相径庭了

current time in day(from 1900:00:00) : 18153cheese had produces : 13 days

这才是小白鼠想要的结果!

于是小白鼠去请教java的创造者java之父

7a150dc6b39857f318f86f316d236ffc.png

原来,实例的初始化也是有讲究的。

1.static字段先设置默认值,其中cheese被设置为null,produceDate被设置为0

2.然后static初始器执行,按照声明出现的顺序执行:

如果先执行cheese的话,调用Cheese()构造方法,此时用produceDate=0为值。

如果先执行produceDate的话,producteDate被设置为2019-09-01,再调用cheese()构造方法。

3 最后从构造器返回cheese类的初始化。

注意:Date设置日期为2019-09-01 为何设置为

new Date(119,8,1)

大家可以进去源码看说明情况

 /** * Allocates a Date object and initializes it so that * it represents midnight, local time, at the beginning of the day * specified by the year, month, and * date arguments. * * @param year the year minus 1900. * @param month the month between 0-11. * @param date the day of the month between 1-31. * @see java.util.Calendar * @deprecated As of JDK version 1.1, * replaced by Calendar.set(year + 1900, month, date) * or GregorianCalendar(year + 1900, month, date). */ @Deprecated public Date(int year, int month, int date) { this(year, month, date, 0, 0, 0); }

其中,year份是从1900年开始的年数,即2019-1900=119

month是0~11计数的,需要实际月份减1,即9-1=8

date 是1~31计数的,实际天就可以 即1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值