1

Why :
If a class does not provide any constructors then default constructor(constructor without parameter) is given by the compiler at the time of compilation but if a class contains parameterized constructors then default constructor is not provided by the compiler.

I am compiling the code below.It gives compilation error.

Code :

class ConstructorTest
{
    // attributes
    private int l,b;

    // behaviour
    public void display()
    {
        System.out.println("length="+l);
        System.out.println("breadth="+b);
    }
    public int area()
    {
        return l*b;
    }

    // initialization
    public ConstructorTest(int x,int y) // Parameterized Constructor
    {
        l=x;
        b=y;
    }

    //main method
    public static void main(String arr[])
    {
        ConstructorTest r = new ConstructorTest(5,10);
        ConstructorTest s = new ConstructorTest();
        s.display();
        r.display();
        r.area();
    }
}

Console Error :

enter image description here

When I invoked only parameterized constructor. Its working fine.but when want to invoke the default constructor with parameterized constructor. Compiler gives compilation error as shown in picture.

Any immediate help will be highly appreciable. Thanks

Razib
  • 10,965
  • 11
  • 53
  • 80
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
  • 3
    Um, so provide your own parameterless constructor - it's as simple as that. The compiler *only* provides a default constructor if you haven't provided one explicitly. – Jon Skeet Jul 20 '15 at 17:26
  • this is a (pretty much) exact duplicate of http://stackoverflow.com/questions/11792207/why-does-the-default-parameterless-constructor-go-away-when-you-create-one-with - that should have the info you want – Kevin W. Jul 20 '15 at 17:27
  • @JonSkeet, I totally agree with you. I just want to know why ? – Debug Diva Jul 20 '15 at 17:28
  • That's just the way the language is defined... If you're asking why the language is designed that way, a lot of your question is redundant. – Jon Skeet Jul 20 '15 at 17:29
  • @JonSkeet, I just want to know the reason behind this. – Debug Diva Jul 20 '15 at 17:30
  • @RohitJindal - You wrote the answer inside the question. Once you provide a parameterized constructor, the compiler does not provide default constructor. You can define a "default" (aka - non parameterized) constructor aside your parameterized constructor. – Avi Jul 20 '15 at 17:30
  • @RohitJindal -you follow this question i think this is more useful for you http://stackoverflow.com/questions/27654167/difference-between-a-no-arg-constructor-and-a-default-constructor-in-java – amila isura Jul 24 '15 at 03:33

5 Answers5

6

The answer to your question is in the paragraph you provided:

but if a class contains parameterized constructors then default constructor is not provided by the compiler.

You have defined a parameterized constructor, therefore the default constructor is not provided by the compiler and therefore must be provided by yourself.

Hari
  • 1,561
  • 4
  • 17
  • 26
Greg Hilston
  • 2,397
  • 2
  • 24
  • 35
  • I agree with you, I want to know why this happen ? if a class contains parameterized constructors then why default constructor is not provided by the compiler ? – Debug Diva Jul 20 '15 at 17:51
  • See the linked @Razib posted (http://programmers.stackexchange.com/questions/257938/why-is-no-default-constructor-generated-if-you-define-an-explicit-constructor) – Greg Hilston Jul 20 '15 at 21:04
1

If you provides a constructor, then the default constructor is not added to your class. You must define it yourself.

JFPicard
  • 5,029
  • 3
  • 19
  • 43
1

You are getting the error while compiling using javac ConstructorTest.java Because the you declare your parameterized constructor - public ConstructorTest(int x,int y). So compiler doesn't provide any default constructor [public ConstructorTest() ] for your class. So you can not call public ConstructorTest() at line 28.

Razib
  • 10,965
  • 11
  • 53
  • 80
  • I agree with you, I want to know why this happen ? if a class contains parameterized constructors then why default constructor is not provided by the compiler ? – Debug Diva Jul 20 '15 at 17:52
  • @Rohit Jindal, you may see the link - http://stackoverflow.com/questions/16046200/why-java-doesnt-provide-default-constructor-if-class-has-parametrized-construc – Razib Jul 20 '15 at 17:58
1

I dont know why you are asking this question. You yourself said "but if a class contains parameterized constructors then default constructor is not provided by the compiler."...so that explains!!

kingshuk basak
  • 423
  • 2
  • 8
  • I agree with you, I want to know why this happen ? if a class contains parameterized constructors then why default constructor is not provided by the compiler ? – Debug Diva Jul 20 '15 at 17:52
0

The reason why is because this allows one to write a struct such as:

struct Test
{ 
    int a;
    double d;
};

It doesn't have a constructor. The user doesn't care if members are initialized. It is mostly use to contain data. It can be then be used via:

Test t;

The end result is less typing. If one cares about how variables are initialized or the logic to initialize them is out of the ordinary, one writes a constructor. It is then assumed a default constructor would do something wrong or unintended so it is then not provided.

One could say the same thing about destructors. If one doesn't care, a default destructor that destroys your members in reverse order and invokes base destructors is provided. If you override it, then the default one is not produced.

edwinc
  • 1,658
  • 1
  • 14
  • 14