what type of java constructors are these? Constructor chaining?
Asked Answered
B

3

1

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;
}
Bettiebettina answered 9/2, 2012 at 2:20 Comment(1)
possible duplicate of How do I call one constructor from another in Java?Soniferous
G
6

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 a Stock, String, and long. 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 with new Date().getTime(), or else just passes nulls.

Note that there must be at least one constructor that does not call this(...), and instead provides a call to super(...) followed by the constructor implementation. When neither this(...) nor super(...) are specified on the first line of a constructor, a no-arg call to super() is implied.

So assuming there isn't more constructor chaining in the Quote class, the third constructor probably looks like this:

public Quote(Stock stock, String price, long timeInMillis) {
    //implied call to super() - the default constructor of the Object class

    //constructor implementation
    this.stock = stock;
    this.price = price;
    this.timeInMillis = timeInMillis;
}

Also note that calls to this(...) can still be followed by implementation, though this deviates from the chaining pattern:

public Quote(Stock stock, String price) {
    this(stock, price, new Date().getTime());

    anotherField = extraCalculation(stock);
}
Generalize answered 9/2, 2012 at 2:25 Comment(1)
@Paul Bellora - hope you dont mind I added your description to the question to give it clearer contextBettiebettina
T
1

This is what we are calling telescoping pattern. But the way you used in Quote class is not useful. For an example think that in your class you have one required property and two optional properties. In this case you need to provide a constructor with that required property and then within that constructor you need to call other constructors with the default values of the optional parameters.

// Telescoping constructor pattern - does not scale well!
public class NutritionFacts {
private final int servingSize; // (mL)  required
private final int servings; // (per container) required
private final int calories; //  optional
private final int fat; // (g) optional
private final int sodium; // (mg) optional
private final int carbohydrate; // (g)   optional
public NutritionFacts(int servingSize, int servings) {
this(servingSize, servings, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories) {
this(servingSize, servings, calories, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories, int fat) {
this(servingSize, servings, calories, fat, 0);
}

I extract this java code from effective java edition 2.

Trillbee answered 9/2, 2012 at 3:12 Comment(0)
J
0

It's calling a constructor that takes an integer argument.

This is something I found on the Java Tutorials Webpage:

You can refer to any member of the current object from within an instance method or a constructor by using this.

In this case it's calling something from the ArrayList class. This was also in the Java Tutorials section:

From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation.

So what your seeing in this case is an Explicit Constructor Invocation

Source: https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

Jinny answered 10/3, 2016 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.