• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Help understanding overloaded constructors

 
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On pages 199 - 200 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, given the code sample:



First of all, why is line 11 allowed? The color string parameter was not listed in the first constructor's parameters. I'm confused about the same thing on lines 20 and 24.

Line 24 also has the error, "The constructor Hamster(int, String) is undefined".

The book describes lines 19 - 21 like this:
"What we really want is for the first constructor to call the second constructor with two parameters. When the constructor with one parameter is called, it creates an object with the default weight and color. It then constructs a different object with the desired weight and color and ignores the new object . That's not what we want. We want weight and color set on the object we are trying to instantiate in the first place."

I'm so confused. I've never seen constructors like this before. What are they trying to show us?
 
Bartender
Posts: 387
47
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line 11 the color field is being assigned a hardcoded value, it is not trying to reference any arguments. Your error on line 24 is only because the constructor you are calling — Hamster(int weight, String color) — is commented out.
 
Jesse Duncan
Bartender
Posts: 387
47
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The book describes lines 19 - 21 like this:
"What we really want is for the first constructor to call the second constructor with two parameters. When the constructor with one parameter is called, it creates an object with the default weight and color. It then constructs a different object with the desired weight and color and ignores the new object . That's not what we want. We want weight and color set on the object we are trying to instantiate in the first place."



The constructor on lines 19-21 is wrong because all it does is create a new object, another object, not the one that it will return. That object is not used in any way, and will immediately become available for garbage collection. It is ignored. The constructor on lines 19-21 does nothing else, so it does not set the fields of the object that it is constructing and will return. The fields of the object that you get back from the constructor will still have their default values. That is what the book is saying.
 
Marshal
Posts: 76419
365
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jj Roberts wrote:. . . all it does is create . . . another object, not the one that it will return. That object is not used in any way . . .

...Until somebody develops an optimisation which “knows” the object is never used and simply skips the whole body of that constructor....

SP: this is the one instance where you shouldn't use /* comments */ for something possibly occupying multiple lines. If there are any other /* comments */ (or */), you will end up commenting‑out half a method/constructor. Fortunately the chances of that sort of code compiling are very small. Use // on each lline instead. Much more reliable.
 
Sam Peterson
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jj Roberts wrote:At line 11 the color field is being assigned a hardcoded value, it is not trying to reference any arguments. Your error on line 24 is only because the constructor you are calling — Hamster(int weight, String color) — is commented out.


In that case, constructors are even more inconsistent than I thought:

I know that constructors and methods are not the same thing, but why do lines 9 to 11 work even though line 5 isn't passing in the String color parameter?

Furthermore, why does line 13 give the error, "Cannot refer to an instance field weight while explicitly invoking a constructor"?
 
Marshal
Posts: 27368
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Peterson wrote:I know that constructors and methods are not the same thing, but why do lines 9 to 11 work even though line 5 isn't passing in the String color parameter?



Line 10 refers directly to the constructor declared in lines 5 to 8, and it passes values of the correct types to that constructor. That's all that's needed. It doesn't matter where line 10 gets those values from. (Except, see your question about line 13.) As for line 5, it isn't passing anything in to anything. Line 5 declares the parameters of the constructor and other code is what passes things into that. Line 10, for example, passes parameters into line 5.

Furthermore, why does line 13 give the error, "Cannot refer to an instance field weight while explicitly invoking a constructor"?



Because you aren't allowed to do that. You probably don't quite understand what "explicitly invoking" a constructor is: line 13 is referring directly to another constructor. This rule doesn't apply to any and all code in a constructor, only to code which explicitly invokes another constructor. That's what line 13 does.
 
Sam Peterson
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:line 13 is referring directly to another constructor. This rule doesn't apply to any and all code in a constructor, only to code which explicitly invokes another constructor. That's what line 13 does.


Makes sense, but then why does the following not work:

Lines 13 and 16 both give the error, "Duplicate method Hamster(String) in type Hamster".

First of all, why is it calling them duplicate methods when they're clearly constructors?

Second, I thought it was possible to write overloaded constructors, so why don't lines 13 - 15 compile?

Finally, why can't line 17 invoke the constructor on lines 13 - 15?
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THe compiler only sees the formal parameter types, not the names of the parameters.  So lines 13 and 16 both look like this:

   Hamster(String)

For any overloading to work, you have to have different method signatures.  Note that the return type of the method is not part of the signature.
 
The first person to drink cow's milk. That started off as a dare from this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic