谁动了我的奶酪?--java实例初始化的顺序问题
故事背景
有一天,老鼠小白發現了一個奇怪的問題,它的奶酪的生產日期被誰搞丟了,不知道奶酪是否過期,可怎么吃呀?
?
讓我們來看看吧
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
這是怎么回事呢?
?
破案
查看代碼提交記錄,發現老鼠小藍有修改記錄,僅僅調整了兩行程序的順序,小白原來的代碼如下:
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之父
?
原來,實例的初始化也是有講究的。
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>.*/@Deprecatedpublic 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
轉載于:https://www.cnblogs.com/davidwang456/p/11518296.html
總結
以上是生活随笔為你收集整理的谁动了我的奶酪?--java实例初始化的顺序问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小白的消费为何被迫升级?-java数据类
- 下一篇: 猿类如何捕获少女心--难以琢磨的try-