In Java how many constructor can we create in one class?
Like with the maximum number of lambdas or the maximum of nested method invocations, we have to make a distinction between the formal Java language specification and technical limits, either due to the formally specified class file format or due to compiler limitations or bugs.
As often, the language specification does not define any limits on the number of constructors. So there’s only the practical limitation that the class declaration must be representable in the byte code format.
Constructors get compiled to special methods (named <init>
), so in the class file, they share a table with ordinary methods, which is limited to 65535 entries. We can max this out by not declaring any ordinary methods. Further, since every constructor must have a distinct signature, each constructor needs its own type signature string in the constant pool, which is limited to 65534 entries on its own.
The constant pool also serves other purposes, like holding the declaration of this class, super class and the name of the Code
attribute, which is needed when having constructors, as well as the linkage information of the super class’ constructor, we have to invoke, so this is the limiting factor on the class file side.
So the minimum constant pool entries needed, are
- super class name (modified UTF8 entry)
- super class (type Class, referring to 1.)
- this class name (modified UTF8 entry)
- this class (type Class, referring to 3.)
- the constructor’s “method” name
<init>
(modified UTF8 entry) - a name&type entry referring to 5. and a super constructor signature (may be shared with one of our constructor’s signature)
- a method entry referring to 2. and 6. (for the super constructor invocation)
- the attribute name
Code
(modified UTF8 entry)
Given these required entries and the limit of 65534 entries (the size plus one is stored as unsigned two byte quantity), we get a class file limit of 65526 constructors and indeed, I could generate a valid class file using the ASM library with that number of constructors and not more.
Actually, you could get more if you name your class java.lang.Object
, as in that special case, there is no super class to declare and no super constructor to invoke. Decide yourself, which actual limit you want to call the maximum number…
As said, there is a 3rd limitation, the compiler implementation. When using a Java compiler, you have to make sure that it doesn’t generate debug information (in case of javac
, use -g:none
) and no other optional attributes which could occupy constant pool entries. But with javac
of JDK11, the performance will drop significantly when you start defining lots of constructors. I got the following compilation times:
1000 constructors: 1 second
2000 constructors: 2 seconds
5000 constructors: 10 seconds
10000 constructors: 1 minute
15000 constructors: 2 minutes
20000 constructors: 4 minutes
30000 constructors: 10 minutes
40000 constructors: 20 minutes
50000 constructors: between 25 minutes and ½ hour
65526 constructors: between 45 minutes and 1 hour
So javac
eventually managed to max out the class file limit, but we may consider a practical limit even before that.
The Eclipse compiler seems to deal better with such source files, but still, maxing out the number of constructors made the IDE almost unusable. With debug symbols turned off and a bit of patience, I managed to compile a class with 65526 constructors with Eclipse. Declaring 65528 constructors produced an error message regarding too many constant pool entries and declaring 65527 constructors revealed a bug in Eclipse, producing a corrupt class file declaring zero constant pool entries (as said earlier, the number is stored as count plus one, so compiler vendors have to keep in mind that the limit is not 65535 but 65534).
You can have 65535 constructors in a class(According to Oracle docs). But IMPORTANTLY keep this in your mind. We achieve this only by CONSTRUCTOR OVERLOADING ( https://beginnersbook.com/2013/05/constructor-overloading/ ). You can create many constructors but with different signatures.
Strictly speaking, the JVM classfile format limits the number of methods (including all constructors) for a class to less than 65536. And according to Tom Hawtin, the effective limit is 65527. Each method signature occupies a slot in the constant pool. Since some of the 65535 pool entries are (unavoidably) consumed by other things, it is not possible for a well-formed class file to use all of the possible method / constructor ids.
Reference - JVMS 4.1 The ClassFile Structure
However, if you are writing sensible Java code the normal way, you won't encounter that limit.
How many should you have? It depends on the classes use-cases. It is often nice to have multiple "convenience" constructor overloads, and implement them using this(...)
to chain to a "master" constructor. (However, you can go over the top. There are N! possible combinations (overloads) of N distinct parameters.)
If you find that you are writing an excessive (subjective!) number of constructors, you should maybe look at alternatives such as the Builder Pattern.
Java Support Constructor Overloading(When java class contain multiple constructors, it is called as constructor is overloaded).A class can have multiple constructors, as long as their signature(parameter) are not the same.So you can define many constructors as you need.There is no limit. Here is a Example:-
class Demo {
private String name;
private String city;
private Double salary;
public Demo() {
}
public Demo(String name) {
this.name = name;
}
public Demo(Double salary) {
this.city = city;
}
public Demo(String name,String city) {
this.name = name;
this.city = city;
}
}