These constructors are overloaded to call another constructor using this(...)
. The first no-arg constructor invokes the second with zero arguments. The second calls the third constructor (not shown), which must take the values Stock
, String
and long
. This template, called the constructor chain, is often used to provide several ways to create an object without duplicating code. A constructor with fewer arguments fills the missing arguments with default values, such as new Date().getTime()
, or simply passes null
s.
Note that there must be at least one constructor that does not call this(...)
, and instead provides a super(...)
call, followed by a constructor implementation. If neither this(...)
nor super(...)
specified on the first line of the constructor, the no-arg call to super()
implied.
Therefore, assuming there is no constructor chain in the Quote
class, the third constructor probably looks like this:
public Quote(Stock stock, String price, long timeInMillis) {
Also note that this(...)
calls may be followed by implementation, although this deviates from the chaining pattern:
public Quote(Stock stock, String price) { this(stock, price, new Date().getTime()); anotherField = extraCalculation(stock); }
source share