These are from the spring amqp samples on github at
https://github.com/SpringSource/spring-amqp-samples.git
what type of java constructors are these? are they a short hand for getters and setters?
public class Quote {
public Quote() {
this(null, null);
}
public Quote(Stock stock, String price) {
this(stock, price, new Date().getTime());
}
as oppossed to this one
public class Bicycle {
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
These constructors are overloaded to call another constructor using
this(...). The first no-arg constructor calls the second with null arguments. The second calls a third constructor (not shown), which must take aStock,String, andlong. This pattern, called constructor chaining, is often used to provide multiple ways of instantiating an object without duplicate code. The constructor with fewer arguments fills in the missing arguments with default values, such as withnew Date().getTime(), or else just passesnulls.Note that there must be at least one constructor that does not call
this(...), and instead provides a call tosuper(...)followed by the constructor implementation. When neitherthis(...)norsuper(...)are specified on the first line of a constructor, a no-arg call tosuper()is implied.So assuming there isn’t more constructor chaining in the
Quoteclass, the third constructor probably looks like this:Also note that calls to
this(...)can still be followed by implementation, though this deviates from the chaining pattern: