There are no static blocks, variables, or methods to worry about.
There are instance blocks, so when called in order, they now give us "tac".
Next we have two user defined constructors. Because of line 9, the first user defined constructor is no longer a default constructor (neither compiler-defined-default nor user-defined-default). But that doesn't explain why the compiler chooses the first constructor (lines 8 - 10) over the second constructor (lines 11 - 12). We can conclude that the first constructor was chosen due to the output being "tacb", but why?
The hardest part to follow is what happens from lines 16 - 18.
Why does the string "f" parameter specified in the Order2 instance call on line 16 not show up in the output? Does it even do anything at all?
What is happening on line 17? Are we assigning our already created order2 object to a new instance? If so, is that why we don't see the "f" parameter as part of the output on line 18?
"order2" is a reference. Initially it references a new object created with "f". Next, the reference is replaced (thereby leaving the old reference for garbage collection) by a new object created with an empty constructor.
The constructor - or for that matter, any overloaded method - that will be called is the one that is the best fit for the arguments supplied (if any) in the method call. Java's overloading rules do not allow ambiguity, and in fact, will give a compiler error if no single method is the best fit, just as well as it will if no method is a fit.
In your sample, you have 2 constructor calls, each of which is a best fit. However, as Carey has noted, you're throwing away the results of the first construction, constructing an entirely new instance of the object and using that one.
Being persecuted doesn't in any way prove your righteousness or your beliefs. Many people get persecuted because they are repugnant or annoying. Or just because they can be.
I think this question is a continuation of this one. I think it started off life in a cert exam book, and as you know, cert exam books are allowed code of the worst style imaginable. Part of the question has to do with how often initialiser blocks run. So I think the throwing objects away is intentional.
Because the two constructors have different numbers of parameters, it is very easy to work out which the compiler will choose just be counting the arguments in the constructor calls.
It's never done THAT before. Explain it to me tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps