views:

88

answers:

5

What is the maximum number of type parameters you used in a java class?

Example for 4:

class MyClass<A, B, C, D> {
}
+3  A: 

Never used more then 2. Actually, I've never seen more then 2 type parameters in any class in java.* package.

Roman
I've used 5, it is rare, but when you have a need for it you can actually make the code simpler. My current code has something like: public interface Parser<S, R, A, X extends ResultTransformer<S, R, A>> where S is the source to transform from, R is type to transform to, A is the additional argument that the transformer takes, and X is the transformer. Once I did that I deleted a huge amount of code and everything got simpler. Not typical, but I have done similar things before with good effect.
TofuBeer
+1 At least more than type parameters makes code calling the class very much unreadable.
Helper Method
A: 

The representation of type parameters in the JDK 1.5 or greater classfile format don't impose any limits on the number of type parameters. So I would say there is no limit. (The information is in this document ... search for the word "signature".)

By contrast, the number of methods, fields and interfaces is 2**16 because the classfile format has a 16 bit counts for these in the ClassFile descriptor.

Stephen C
A: 

I have a Quadruple class which is a poor man's tuple. Obviously, it takes four parameters.

quant_dev
+1  A: 

I think I was once trying to create a framework for board-based games that took 7 type parameters. I never completed it but it’s quite probable that that list would have grown some more. :)

public interface Board<B extends Board<B, C, M, P, Pi, S, T>, C extends BoardColor, M extends Move<M, P, Pi, S>, P extends BoardPlayer<B, C, M, P, Pi, S, T>, Pi extends BoardPiece<B, C, M, P, Pi, S, T>, S extends BoardState<B, C, M, P, Pi, S, T>, T extends BoardTile<B, C, M, P, Pi, S, T>> {
    …
}
Bombe
A: 

When using Functional Java library, you quite often need four to five type parameters. For instance, see this example. More examples here.

missingfaktor