Difference between a no-arg constructor and a default constructor in Java
Asked Answered
R

8

25

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?

Router answered 26/12, 2014 at 7:29 Comment(3)
no it doesn't invoke default constructor now there is no default constructorAnna
Please also refer to Java Default ConstructorFlynt
my two cents: The default constructor can call only a no-argument constructor of a base class.Nomination
O
31

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.

Ogg answered 26/12, 2014 at 7:32 Comment(4)
I am a big fan of any call to supper()Devondevona
@JonathanLandrum Is there really a method in Java as supper( ) ?Parton
" If you implement any constructor then you no longer receive a default constructor." - So after adding my own constructor, should developer type the no-arg constructor again or Java implicitly adds it?Parton
@user3705478 sorry for the misunderstanding; that comment was meant as a joke. The English word "supper" is an archaic term for a meal, especially in the evening and with company. Likewise, the "call to supper" was a notification that the meal is ready.Devondevona
J
8

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.

Jaynejaynell answered 26/12, 2014 at 7:38 Comment(0)
F
5

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,

public Test(){
     super();
     this.setSize(200,200);
     this.setVisible(true);
   }

So there is no default constructor now to be invoked by the JVM.

Flynt answered 26/12, 2014 at 7:44 Comment(0)
B
2

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:

Class Base {}

Class Derived extends Base {} // Default constructor of class "Derived" will automatically calls non-arg constructor of class "Base" and intialzes value to the variables

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:

Class Base {

int y;
    public Base() {
    }
    public int getY() {
    return y;
    }

}

public class Derived extends Base {
 private int x;

 public Derived() { //super() will be automatically used
 }

 public int getX() {
 return x;
 }

 public void setX(int x) {
 this.x = x;
 }
}

Derived d = new Derived();
 System.out.println("Base x value => " + d.getX());

System.out.println("Base y value => " + d.getY());

Result:

Base x value => 0 // Default value to its primitive datatype(In this case: int)

Base y value => 0
Botzow answered 11/3, 2017 at 23:1 Comment(0)
V
1

The default constructor is as its name, if we do not create a constructor explicitly then the java compiler will create a default constructor. As a developer, if we create our own constructor with 0 arguments then it will be N.o argument constructor.

Vinic answered 23/10, 2020 at 7:37 Comment(0)
E
1

It's easy to confuse the concepts of "default constructor" and "no-argument constructor" in Java. The reason is that a default constructor is a particular kind of no-argument constructor. However, not all no-argument constructors are default constructors.

If any constructor appears in the source code of a class, even an empty no-argument constructor, a default constructor is not generated and does not exist. Because Test declares a constructor, it does not have a default constructor, so Test cFrame = new Test(); does not call a default constructor.

These two classes are identical in terms of behavior (both have a no-argument constructor that does nothing), but one has a default constructor and the other does not.

class NoDefaultConstructor {
    NoDefaultConstructor() {
        // This is a no-argument constructor with an empty body, but is not a default constructor.
    }
}
class HasDefaultConstructor {
    // This class has no declared constructors, so the compiler inserts a default constructor.
}

In each case, you can create an instance with new NoDefaultConstructor() or new HasDefaultConstructor(), because both classes have no-argument constructors, but only HasDefaultConstructor's no-argument constructor is a default constructor.

Style note: If a constructor doesn't explicitly chain to another constructor with a call to super(...) or this(...), a call to super() is automatically inserted at the beginning of the constructor. You never need to write super() in a Java constructor. Doing so adds visual noise and is unnecessary.

Eskil answered 9/9, 2021 at 22:20 Comment(0)
I
0

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.

Isabea answered 14/1, 2019 at 23:52 Comment(0)
S
0

Default constructor inherit the access modifier of class which means if a class is public then default constructor must be public. In other hand No arg constructor the access modifier can be public,private,protected and default .

Synergy answered 22/6 at 5:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.