Search code examples
javaconstructordefault-constructor

Using default Constructors in java, even if the parameterized constructors are present


I just wanted to clear my concept here, so i am asking...

If I define an explicit parameterized constructor for my class, can I still invoke the default constructor provided by the java compiler (which is provided for every class by default) ??

Or does it cause result in a compile time error in such case?? Please explain what exactly happens with respect to the calls made by the compiler !!


Solution

  • If and only if no constructor is provided, then a no-argument constructor is created by the compiler.

    The JLS states in Chapter 8:

    If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.

    The one "catch" is:

    It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible constructor (§6.6) that takes no arguments and has no throws clause.