In other words, what would be the problem if Java compiler provided a default constructor, even in the case when there also is a constructor with args?
-
4You don't *have to* provide a no-arg constructor. – assylias Mar 20 '19 at 18:40
-
1@assylias You do if you only provide a constructor that has arguments, and someone tries to create an instance of your class using a no-argument constructor. – FredK Mar 20 '19 at 18:49
-
@FredK You still don't *have* to define one.. let them get a compile error for using your class incorrectly. *Not* defining a no-arg constructor is completely valid. – xtratic Mar 20 '19 at 18:58
-
@xtratic True, but the OP was asking WHY doesn't the compiler automatically provide a no-argument constructor when you provide a constructor with arguments. The answer is precisely what you point out: If I don't want to allow a no-arg constructor, it would be impossible to do if the compiler automatically provided one. – FredK Mar 20 '19 at 19:04
2 Answers
Generally speaking, Java will provide a no-arg constructor if none is provided.
If you have an "arg" constructor and don't try to use the no-arg constructor (or define one) you have no problem.
However, the Java Serialization library requires you to have a no-arg constructor so it knows how to create an object when it is deserialized.
An alternative which I take with my library is to call the no-arg constructor if it exists, or just create the object without calling a constructor if it doesn't. This can the side effect that fields are not initialised if you don't call a constructor. e.g.
class MyClass {
// only set if you call a constructor!
transient final List<String> list = new ArrayList<>();
int a;
MyClass(int a) { this.a = a; }
}
In this situation, not having a no-arg constructor means it is hard to know how to deserialize it correctly.

- 525,659
- 79
- 751
- 1,130
...even in the case when there also is a constructor with args?
Then there'd be no way for you to prevent your class from having a no-args constructor other than declaring a private one (which isn't quite the same as it not having one).
Having only a constructor accepting arguments is a common use case. Making you declare a private no-args constructor in that common case wouldn't make much sense.
In contrast, wanting to have no constructors for a class is an uncommon use case. So providing one when you don't provide any explicitly makes (some) sense (though frankly I'd be just as happy if it didn't provide a default at all).

- 1,031,962
- 187
- 1,923
- 1,875
-
1There's no way to prevent your class from having any constructor, other than declaring a private one. – shmosel Mar 20 '19 at 18:48
-
1@shmosel - I said that above. I'm not sure what you're clarifying... – T.J. Crowder Mar 20 '19 at 18:52
-
You said there'd be no way to prevent a class from having a no-arg constructor. I'm pointing out that there are cases where you don't want a constructor at all, and there's no way around that other than declaring a private constructor. – shmosel Mar 20 '19 at 18:56
-
1
-