Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Joe Ess
  • salvin francis
  • fred rosenberger

How to know which constructor will automatically be used

Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class with 2 user defined constructors:

The output is:

We start out with "t" from line 4.

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?
Saloon Keeper
Posts: 6772
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
Saloon Keeper
Posts: 21603
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Posts: 67464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!