Can I define `default constructor` in Java?

If you explicitly define any constructor, then it cannot be a default constructor, even if you code one that is exactly equivalent to default constructor generated by the compiler. Default, here, means in the absence of any action by you the programmer.

UPDATE: OP wants an answer based on evidence

Compiler Rules from Section 13.4.12 Method and Constructor Declarations (JLS8):

  1. If you declare no constructors in your class, then a default constructor is generated by the compiler.

Evidence: If the source code for a non-inner class contains no declared constructors, then a default constructor with no parameters is implicitly declared (§8.8.9).

  1. If you do declare one or more constructors in your class, even if, it is a no-arg constructor and so akin to the compiler generated default constructor, your explicit constructor will replace the compiler generated default constructor. To stress this further, your explicit no-arg constructor which is equivalent to the compiler generated one is not the compiler generated one.

Evidence: Adding one or more constructor declarations to the source code of such a class will prevent this default constructor from being implicitly declared, effectively deleting a constructor, unless one of the new constructors also has no parameters, thus replacing the default constructor.

Leave a Comment