Difference between a no-arg constructor and a default constructor in Java

amila isura picture amila isura · Dec 26, 2014 · Viewed 55.4k times · Source

Actually I can not understand that what is the difference between a no-arg constructor and a default constructor.

import javax.swing.*;

public class Test extends JFrame {
   public Test() {
     super();
     this.setSize(200,200);
     this.setVisible(true);
   }
   public static void main(Sting[] arg) {
       Test cFrame = new Test();
   }
}

Does this invoke the default constructor of this class while creating Test object called cFrame?

Answer

Elliott Frisch picture Elliott Frisch · Dec 26, 2014

The default constructor is a no-args constructor that the Java compiler inserts on your behalf; it contains a default call to super(); (not supper()) which is the default behavior. If you implement any constructor then you no longer receive a default constructor.

JLS-8.8.9. Default Constructor says (in part),

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

If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.