2

What is the difference between this(var) and this.var in JAVA.?

public class Clock {

    private long time = 0;

    private Clock(long time) {
        this.time = time;
    }

    public Clock(long time, long timeOffset) {
        this(time);
        this.time += timeOffset;
    }

    public static Clock newClock() {
        return new Clock(System.currentTimeMillis());
    }
}
alex
  • 8,904
  • 6
  • 49
  • 75
  • 2
    this() calls another constructor. `this.` means use the field on `this` object and `var` is a keyword in Java 10. To see when each line of code does I suggest you step through the code in your debugger. – Peter Lawrey Mar 21 '18 at 08:24
  • 1
    Check [that answer in the duplicate](https://stackoverflow.com/a/3728188/4391450) to get a complete set of `this` meaning. – AxelH Mar 21 '18 at 08:37
  • 1
    @PeterLawrey `var` is not a keyword in Java 10, it is a reserved type name (see [JEP-286](http://openjdk.java.net/jeps/286)) – Mark Rotteveel Mar 23 '18 at 20:24
  • @MarkRotteveel thank you for the correction. – Peter Lawrey Mar 24 '18 at 09:39

2 Answers2

4

Given the class

public class Clock
{
    private long time = 0;

    public Clock(long time)
    {
        ...
    }
}

you use

  • this(x) to call the constructor with the parameter x. This is called constructor chaining and you can only call this() from a constructor, where it has to be the first statement. Contructors may not call themselves through constructor chaining.
  • and you use this.time = x to set the member called time to the value of x. this indicates the scope of time, e.g. if you are in a method with a local variable time, using time will get the closest scope from the method, which is the local one. If you want the instance scope, you use this.time.

For further reading, see Java Language Specs - 8.8.7 Constructor Body and Java Language Specs - 6.3 Scope of a declaration as well as the following sections.

Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
  • 2
    Note that `this()` only works from the constructor **as a first statement!!**, it is not allowed inside a method. – AxelH Mar 21 '18 at 08:31
  • Thank you, I wasn't quite sure about that. So it's constructor chaining for every use of `this`. – Thomas Flinkow Mar 21 '18 at 08:32
  • 2
    Check [JLS 8.8.7. Constructor Body](https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.7) to complete your answer. – AxelH Mar 21 '18 at 08:35
  • 1
    To improved the second explanation. `this.time` allow you to specify the scope. if you are in a method with a local variable `time`, using `time` will get the "closest" scope from the method, which is the local one. If you want the instance scope, you use `this.time`. It become a bit more complex when we are in an inner class... I will not go deeper here ;) – AxelH Mar 21 '18 at 08:43
  • @AxelH thank you very much for your additions. I just brazenly copied parts of your comment into my answer, I hope that's okay for you. Almost my whole answer is your contribution anyways ;-) – Thomas Flinkow Mar 21 '18 at 08:48
  • 1
    Nah, that's fine. Your answer was already quite good so there were no point to create my own at this point. – AxelH Mar 21 '18 at 08:51
2

The first one (this(var)) calls the constructor with the var as parameter, while the second one simply references the var property within the object.

Robert Kock
  • 5,795
  • 1
  • 12
  • 20