8

Why Java doesn't provide default constructor, if class has parametrized constructor? Consider the following example

class A {
    int a;

    public A() {
    }

    public A(int val) {
        a = val;
    }
}

Here I explicitly need to add default constructor. Is there any reason, why Java doesn't provide default constructor for class having parametrized constructor?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
rigel
  • 81
  • 1
  • 3
  • I don't think this is a duplicate; it's asking *why* the specification says what it does. – OrangeDog Nov 19 '14 at 21:12
  • @OrangeDog The difficulty here is that the literal answer to the OP's question is *"A compiler does not generate a default constructor when you explicitly define a constructor because the definition of a default constructor is 'one the compiler defines when you do not define another one'"*. (Their `public A() {}` is not a default constructor.) The OP was probably mixing up terminology and *probably* actually asking about no-argument constructors. Maybe the question should be edited if we are to assume the OP was mixing up terminology. – Radiodef Nov 19 '14 at 22:07

4 Answers4

10

The reason has to do with a combination of security and interface. The compiler shouldn't give you methods you don't explicitly define. The one exception is a convenience no-arg constructor if you don't specify any constructors. If you do specify a constructor the compiler assumes you don't want any others.

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
6

Because it would break the class design if the compiler would automatically provide no arg constructor to every class. Consider the Scanner class. It has few constructors, where you can specify from where you want to read the data. If the compiler would also add no arg constructor, then, after invoking one of Scanner's method for reading data from an object initialized with this constructor an exception would be thrown, because the data source would not be specified.

luke657
  • 826
  • 2
  • 11
  • 28
2

Java will give you a default constructor if your class doesn't define any.

If you have any constructor defined, (even one without any arguments) the compiler will not give you another one.

It works like this because it was specified this way. You can read more about this in the Language Specification

Rodrigo Sasaki
  • 7,048
  • 4
  • 34
  • 49
  • 6
    OP knows that `Java doesn't provide default constructor, if class has parametrized constructor`. Question is "why"? – Pshemo Apr 16 '13 at 20:29
2

There are situations in which it would be undesirable to have a default constructor. The language has to provide some way to tell the compiler not to generate it. Using some indicator other than the presence of an explicit constructor would lead to issues such as the need to specify a constructor whenever the default is disabled.

The current arrangement eliminates that problem by ensuring there is another constructor whenever the default is disabled.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75