constructor with more than 255 params

J

James

Hi,

My code MANDATES me to use 300+ params in the constructor. The java code
which is generated via IDL gives compiler problems.

Cna anyone suggest what can be done if contructor needs to handle more than
255 params?

thanks in advance
jim
 
I

Ingo R. Homann

Hi,
Hi,

My code MANDATES me to use 300+ params in the constructor. The java code
which is generated via IDL gives compiler problems.

Cna anyone suggest what can be done if contructor needs to handle more than
255 params?

thanks in advance
jim

Change the design, e.g.:

void foo(Map<String,Object> params, ParamClass otherParams, String[]
moreParams) {...}

class ParamClass {
int p1,p2,p3;
String p4,p5,p6;
}

(Of course with more meaningful names! :)

Honestly: It is in *no* case useful to have so many parameters without
"grouping" them by one of the ways explained above!

Ciao,
Ingo
 
R

Roedy Green

My code MANDATES me to use 300+ params in the constructor. The java code
which is generated via IDL gives compiler problems.

Compose it in chunks and pass the chunks as objects to your
constructor.

Alternatively, fill in the major stuff in the constructor, and fill in
the rest with setters.

Alternatively, put the parameter info into a Collection, perhaps a
HashMap of name=value pairs and pass in the Collection.
 
J

James

Hi,

Thanks all for the quick replies.

Is there a limit on number of member variables in a java class?

thanks once again in advance
jim
 
J

juhunu

Any idea whether
Such a restriction is for constructors only? or for all the methods in
Java?
 
H

Hemal Pandya

Roedy said:
he is just using setters, but chaining them together in one line. To
do that a setter would have to return this.

Yes. Also, the setters (at least in this scheme) do not have the
standard "set" prefix either. Is that bad?

The idiom was first (I think) suggested by Bjarne Strostrup, as an
alternative to introducing "named parameters" in C++ and described in
his C++ Programming Language book. It may not be very applicable to
Java.
 
T

Tor Iver Wilhelmsen

Any idea whether Such a restriction is for constructors only? or for
all the methods in Java?

Since constructors are synthesized into semi-ordinary void methods
called <init>, the restriction would be on method parameters.
 
T

Tor Iver Wilhelmsen

Roedy Green said:
he is just using setters, but chaining them together in one line. To
do that a setter would have to return this.

That they don't is a badness Sun should fix in the JavaBeans spec.
StringBuffer/StringBuilder and quite a few XML APIs realize why
returning this instead of void for mutators is a good idea.
 
T

Thomas Hawtin

You run into a problem with subtypes when returning this. One solution,
from Java 5.0, is to give an abstract base class a generic type
Yes. Also, the setters (at least in this scheme) do not have the
standard "set" prefix either. Is that bad?

It's a bit pointless to have set on every method. It's going to be a bit
obvious what is going on. Better off without the clutter. Particularly
if you have a separate builder class. There's no need to make the
original class mutable - either have a separate builder with a create
method or pass an arguments object to the target class constructor.

Tom Hawtin
 
R

Roedy Green

Yes. Also, the setters (at least in this scheme) do not have the
standard "set" prefix either. Is that bad?

These things are not setters in the Java sense, so it would be not
correct to label them as such.

What you really want are properties like Delphi has -- the same syntax
for public variables as for get/set so you con change the
implementation without changing client code.

See http://mindprod.com/jgloss/bali.html

You also want factoring the way you have with Abundance "moods".

So you can run a number of methods on the same object without having
to repeat the object, or a method on a number of objects, without
having to repeat the method name.

In Abundance you would say:

write person address zip

which amounts to
person.write(); address.write(); zip.write();
 
A

Andrew McDonagh

James said:
Hi,

My code MANDATES me to use 300+ params in the constructor. The java code
which is generated via IDL gives compiler problems.

Cna anyone suggest what can be done if contructor needs to handle more than
255 params?

thanks in advance
jim

can you post this constructor - we don't need to see anything
else....though the IDl would be good too

cheers

Andrew
 
H

Hemal Pandya

Thomas said:
You run into a problem with subtypes when returning this. One solution,
from Java 5.0, is to give an abstract base class a generic type
parameter for the derived class (class Base<THIS extends Base>) and use
the generic parameter for return types.

Don't know what problem you are referring to , but I don't think I am
going to be extending a "Parameters" class.
It's a bit pointless to have set on every method. It's going to be a bit
obvious what is going on.

Yes, I don't think the prefix makes sense for Parameters class.
Better off without the clutter. Particularly
if you have a separate builder class.

Separate Paramaters class is the whole idea of this idiom.
There's no need to make the
original class mutable - either have a separate builder with a create
method or pass an arguments object to the target class constructor.

Well, the original class may have to be mutable for its own reasons.
But yes, no need to make it mutable so that set/init methods can be
called for initial creation.
 
B

Bryce

Hi,

My code MANDATES me to use 300+ params in the constructor. The java code
which is generated via IDL gives compiler problems.

Cna anyone suggest what can be done if contructor needs to handle more than
255 params?

If you worked on my team, you'd have to justify the use of 300+
params. I would say to go back and look at the design again, and make
sure there's no other way.
 
T

Thomas G. Marshall

Bryce coughed up:
If you worked on my team, you'd have to justify the use of 300+
params. I would say to go back and look at the design again, and make
sure there's no other way.

I'm still dying to find out just what the heck he views as the compelling
reason for this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top