故事背景

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

让我们来看看吧

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) : 18153
cheese had produces : 18153 days

这是怎么回事呢?

破案

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

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) : 18153
cheese had produces : 13 days

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

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

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

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 <code>Date</code> object and initializes it so that
* it represents midnight, local time, at the beginning of the day
* specified by the <code>year</code>, <code>month</code>, and
* <code>date</code> 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 <code>Calendar.set(year + 1900, month, date)</code>
* or <code>GregorianCalendar(year + 1900, month, date)</code>.
*/
@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

参考资料

【1】https://docs.oracle.com/javase/specs/jls/se12/html/jls-12.html#jls-12.4

最新文章

  1. URL的组成格式
  2. Busybox下tftp命令使用详解
  3. ASP.NET MVC Json()处理大数据异常解决方法,字符串的长度超过了为 maxJsonLength
  4. 利用HTML5开发Android(6)---构建HTML5离线应用
  5. Process.StandardInput属性
  6. [LeetCode#277] Find the Celebrity
  7. Apache服务器部署多个进程
  8. JQ在线引用地址
  9. 面向对象编程总结--Python
  10. Naive RNN vs LSTM vs GRU
  11. 人撒娇地撒基督教扫ID祭扫我京东is啊单间
  12. Qt5对XML文件操作
  13. 2017-12-18python全栈9期第三天第一节之昨天内容回顾与作业讲解用户三次机会再试试
  14. Sprng4之JDBC--很原始的使用方法
  15. .Net拾忆:从List去除重复-拾忆集合
  16. (转)Cognos的下载地址分享
  17. git概念及工作流程详解
  18. resource not found :rgbd_launch
  19. Python学习day5作业
  20. 【SDOI2011】工作安排

热门文章

  1. MongoDB之数据库备份与恢复
  2. 定时器任务django-crontab的使用【静态化高频率页面,增加用户体验】【系统的定时器,独立于项目执行】【刘新宇】
  3. 使用JMS接口接入WebSphere MQ消息
  4. 简述关于ASP.NET&#160;MVC与.NET CORE 的区别
  5. Java开发包Jedis
  6. QMS 的趨勢概述
  7. redpwnctf-web-blueprint-javascript 原型链污染学习总结
  8. U盘制作启动盘后空间容量变小解决方法
  9. 使用Counter进行计数统计
  10. 06 css选择器