[JAVA] About this ()

I didn't understand the meaning of this (), so I'll write it down.

Example of use

Code that calculates the time when the program was executed.


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
    System.out.println(new Date());        

When you run


Fri Feb 28 14:22:03 UTC 2020     

Checking what's happening with new Date ()


public class Date
    implements java.io.Serializable, Cloneable, Comparable<Date>
    private static final BaseCalendar gcal =
    private static BaseCalendar jcal;

    private transient long fastTime;

    private transient BaseCalendar.Date cdate;

    private static final long serialVersionUID = 7523967970034938905L;

    public Date() {

    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.


    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.

Summary of this ()

-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

About this ()
About =
About method.invoke
About Kotlin
About attr_accessor
About Hinemos
About inheritance
About params
About Docker
About Rails 6
About form_for
About Spring ③
About enum
About polymorphism
About Optional
About JitPack
About Dockerfile
About devise
About encapsulation
About Docker
About active_hash
About static
About exceptions
About scope
This and that about Base64 (Java)
[Maven] About Maven
About exception handling
Review about bundler
[Java] About Java 12 features
About Rails routing
About cyclomatic complexity
About exception handling
About AsyncTask Generics
About Ruby symbols
About array multiplication
[Java] About arrays
About HotSpot VM
About ruby ​​form
About Active Storage
About Spring AOP
About singular methods
About getter setter
About build tools
About MacinCloud introduction
[Note] About nil
About keyword arguments
Chew about API
[About double hash]
About installing Cocoapods
Something about java
Where about java
About HttpServlet () methods
About ActiveRecord :: PendingMigrationError
About SIGNING_REGION: NoSuchFieldError
About standard classes
About spring AOP
About Ruby arrays
About Ruby inheritance
About Java threads