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?
The
default
constructor is a no-args constructor that the Java compiler inserts on your behalf; it contains adefault
call tosuper();
(notsupper()
) 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),
Answer is No. Reference variable cFrame will call non-arg constructor Test(), not default constructor. While Test() constructor will further find and call non-arg constructor of JFrame class(Parent) and so on Every class must have at least one constructor. If not declared explicitly, java compiler provides a non-parameterised constructor, i.e, default constructor. This default constructor calls its parent class’s non-parameterised constructor It initializes class variables to their default values.
Eg:
While non-arg constructor is defined by a programmer only. It can also intializes the variables. One more point to add here is that super() call is automatically added by the java compiler, if doesn’t find super() in derived class.
Eg:
Answer to your question is No. Java won't provide a default constructor if you write any kind of constructor in class.
One difference between them is that the body of default constructor will always be empty whereas we can insert our own code in no-arg constructor.
The
default constructor
is a constructor that the Java compiler adds to your code if no explicit constructor is available. The default constructor invokes the super class constructor with no args.If you have added your own constructor (no matter whether it's without parameters or with parameters) the compiler will not add the default constructor in this case.
What is a default constructor ?
It is a constructor that is added by the compiler if you have not defined a constructor.
If your class has a constructor already then the compiler will not add the default constructor.
So in your case you have the constructor,
So there is no default constructor now to be invoked by the JVM.