Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion with Constructors-Please respond  RSS feed

 
Madhoo good
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look at the following code, i have to call two different constructors in ClassTwo,is there any better approach so that i can avoid passing dummyVariable in the other constructor.or is the current approach correct?


public class ClassOne{

protected Vector items;
protected Vector otherItems;
public ClassOne(Vector items) {
this.items = items;
}

/* here boolean dummyVariable is not needed but i'm passing as to differntiate these two constructors
as per req,items and otherItems have to be in two different constructors*/

public ClassOne(Vector otherItems,boolean dummyVariable)
{
this.otherItems = otherItems;
}

}


public class ClassTwo{
ClassOne co = new ClassOne(items)
ClassOne co = new ClassOne(otherItems,true);
}

Thanks for the any response.
-GoM
 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are defining the constructor to accept two arguments, then when you create an object you are supposed to pass the respective number and type of arguments... so it is upto you to customise the defnition of the constructor..

and this is what i am able to observe from your post.. i am not that clear of your post.. if you could be even more legible i think we could help u better..
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boolean dummyVariable is not needed but i'm passing as to differntiate these two constructors
as per req,items and otherItems have to be in two different constructors
--


I am not getting you exactly what you want either you can do one thing you change one of the parameter type: 1) Items 2) OtherItems

CHange one parameter of any other type......
This will make your requirements fulfill....

Hope this will help you if still any concern then revert me back.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Madhoo good:
Please look at the following code, i have to call two different constructors in ClassTwo,is there any better approach so that i can avoid passing dummyVariable in the other constructor.or is the current approach correct?


There very likely is a better approach, yes. In fact it's already a questionable design to have two fields, of which only one is needed in each case.

Can you tell us more about what this class is doing, and why you need the two different fields?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And do you *have* to use two different constructors, all the work could be done in the second constructor:
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to disambiguate constructors is to avoid the problem by relying on static methods instead, and name those methods differently:

Notice I could have instead provided a public constructor with a flag:

public Point(double x, double y, boolean cartesan)

But then you have to remember if that flag is true==cartesian or true=polar when staring at code. I find it better to have a static method that spells it out, instead of relying on a flag hack.
[ May 08, 2006: Message edited by: Jeff Albertson ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff, yes, that is a good pattern.

Notice, though, that in your example, both versions will end up with using both fields, whereas in the original question always only one of two fields will be used, depending on which constructor is called. To me that indicates that there might be a more fundamental design problem here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!