• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor chaining question.  RSS feed

 
Gary Charles
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In the class below I'm trying to create a class that will accept dates in various formats and create a range. The first constructor is easy because I send it the begin date and end date as Date objects. Now I want to send a month(and year) in a constructor and derive the begin and end dates from it. In my constructor that accepts the month/year I need to put the at the top to be allowed, but the parameters are not built yet. I've seen chaining examples on the www, but I haven't been able to get what I've seen working for me. How do I do this?



Thanks,
Gary
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Not all constructors can be chained. If you have two completely different ways to construct an instance of the class, then they may have to be completely independent constructors.

Henry
 
Gary Charles
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Not all constructors can be chained. If you have two completely different ways to construct an instance of the class, then they may have to be completely independent constructors.


If I don't chain here, how do I use and ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Charles wrote:
If I don't chain here, how do I use and ?



Can you elaborate a bit? What is wrong with having two overloaded constructor -- that aren't chained to each other?

Henry
 
Gary Charles
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Can you elaborate a bit? What is wrong with having two overloaded constructor -- that aren't chained to each other?


Oh my goodness. I was making it much harder than it needs to be.
I can do this...

and


Thank-you for your patience and time.
Gary
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!