Who Moved My Cheese? Examples of problems initialization sequence --java

story background

One day, little white mice discovered a strange question, its cheese production date whom have not lost, I do not know whether expired cheese, how to eat it?

Who Moved My Cheese?  Examples of problems initialization sequence --java

 

Let's look at it

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");

}
}

White as expected, and the program ran out of cheese listed how many days, but the printed results do not expire cheese

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

This is how it happened?

Who Moved My Cheese?  Examples of problems initialization sequence --java

 

Solve a case

View Code to submit records and found that the mice have small blue modify records, only adjust the order of the two lines of the program, white original code is as follows:

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");

}
}

Just modify the order of the two variables, the output result of the differ

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

This is the desired outcome mice!

So to ask the parent mice creator of java in java

Who Moved My Cheese?  Examples of problems initialization sequence --java

 

It turned out that the instance initialization is a luxurious.

1.static field to set a default value, wherein the cheese is provided to null, produceDate is set to 0

2. Then static initiator are performed in the order statement that appears:

If the cheese, then the first implementation, calls Cheese () constructor, this time with produceDate = 0 value.

If the first execution produceDate words, producteDate is set to 2019-09-01, then call cheese () constructor.

3 finally returned from the initialization cheese class constructor.

Explanation

Date Set the date to be set to why 2019-09-01

new Date(119,8,1)

We can look into the source code to explain the situation

 /**
 * 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);
 }

Among them, parts of year is the number of years from the beginning of 1900, ie 2019-1900 = 119

month 0 to 11 counts, it requires actual month minus 1, i.e. 9-1 = 8

date count from 1 to 31, i.e. the actual days to 1

Reference material

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

Guess you like

Origin www.cnblogs.com/davidwang456/p/11518296.html