aliamiri aliamiri - 2 months ago 10
Java Question

' this' in constructor

what is 'this' here? I have only this two constructor

public DefaultSpeculator(Configuration conf, AppContext context) {
this(conf, context, context.getClock());
}

public DefaultSpeculator(Configuration conf, AppContext context, Clock clock) {
this(conf, context, getEstimator(conf, context), clock);
}

Answer

Say you had a constructor:

public Something(String foo) { ... }

And you had another overloaded constructor:

public Something() { ... }

You can invoke the other constructor like this:

public Something() {
    this("foo");
}

This will execute the former constructor with String parameter "foo" passed.

In your case, your first constructor invokes the second constructor with some arguments. The second constructor invokes a constructor of four arguments.

Comments