I didn't understand the meaning of this (), so I'll write it down.
Code that calculates the time when the program was executed.
qiita.java
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
System.out.println(new Date());
When you run
qiita.java
Fri Feb 28 14:22:03 UTC 2020
Checking what's happening with new Date ()
Date.class
public class Date
implements java.io.Serializable, Cloneable, Comparable<Date>
{
private static final BaseCalendar gcal =
CalendarSystem.getGregorianCalendar();
private static BaseCalendar jcal;
private transient long fastTime;
private transient BaseCalendar.Date cdate;
private static final long serialVersionUID = 7523967970034938905L;
public Date() {
this(System.currentTimeMillis());
}
public Date(long date) {
fastTime = date;
}
}
came out. this (). System.currentTimeMillis () seems to be a method that returns the OS time. This () is called by the constructor below this.
Date.class
public Date(long date) {
fastTime = date;
}
You can see that it is overloaded. This constructor just puts time in the fields. In other words, this () meant calling a constructor with the same arguments.
-This () is used when calling the constructor. -Determine which constructor to call based on the argument. -Can be used when you want to set a default value.
Recommended Posts