日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

谁动了我的奶酪?--java实例初始化的顺序问题

發布時間:2025/4/5 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 谁动了我的奶酪?--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) : 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>.*/@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实例初始化的顺序问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。