What is the maximum number of arguments supported in a java constructor. I am using android studio. I am getting a too many parameters error when I use above 300 parameters.
Asked
Active
Viewed 675 times
0
-
6why on earth would you need 300 parameters? it's not Noah's Arc. you don't have to fill all in just one tiny little Object – Stultuske Sep 14 '18 at 05:54
-
9*I use above 300 parameters* - WTF, glad I don't have to maintain your code – Scary Wombat Sep 14 '18 at 05:54
-
@Stultuske Defensive programming? Maybe the OP expects another Y2K bug at some point? :-) – Tim Biegeleisen Sep 14 '18 at 05:55
-
Use a property or any other configuration file and read it instead of dealing with 300+ parameters. – Glains Sep 14 '18 at 05:58
-
@TimBiegeleisen even then. It's not a pokémon game where one constructor has to "catch 'm all" :). Even if all this information is needed for one instantiation, I would believe that it would at least be possible to group some of the data in a composite Object, instead of passing them all seperately – Stultuske Sep 14 '18 at 06:00
-
@Glains usually, parameters are decided by the flow of the application. hard to put into property files what you can't know up forehand – Stultuske Sep 14 '18 at 06:01
-
@Stultuske Actually i tought i have read it was in the `main` method. Of course, then you should think about refactoring your class. – Glains Sep 14 '18 at 06:03
-
OP, really interested to know your use-case whereby you need so many.. I usually get irritated if I have around half a dozen – Scary Wombat Sep 14 '18 at 06:03
-
@AkhilS Could you please provide a use case where you actually need 300 constructor parameters? I'm very curious. – MC Emperor Sep 14 '18 at 06:17
2 Answers
1
According to the JVM docs:
The number of method parameters is limited to 255 by the definition of a method descriptor (§4.3.3), where the limit includes one unit for this in the case of instance or interface method invocations.
I guess it's the same with constructors. Either way, your code needs some refactoring to do if it really has a method with over 10 arguments.

Nikita Karamov
- 517
- 1
- 5
- 18
-
let's assume your opinion on this is shared over methods and constructors. I guess "over 300" qualifies as "over 10" :) – Stultuske Sep 14 '18 at 05:59
-1
method( YourCustomObjectContaining301params o301p )
is all you need

iPirat
- 2,197
- 1
- 17
- 30
-
2I would not want to maintain a class with 300+ params... (also, does that object have a constructor with 300+ params as well?) – Glains Sep 14 '18 at 06:00
-
-
-
@Stultuske Thats why i put it in brackets ;) Anyways, suppose one or more properties are required. Same thing as Springs constructor vs setter injection... – Glains Sep 14 '18 at 06:04
-
@ScaryWombat not necessarily so. if all values must be known before termination of instantiation, for instance. A constructor that must log/throw/handle/... an error if not all data is present – Stultuske Sep 14 '18 at 06:13
-