Mathews Mathai
Mathews Mathai

Reputation: 1707

What is the actual use of the default constructor in java?

I have read in many sources and books that constructor is used to initialise the fields when an object is created. I have also read that JVM provides a default constructor if I don't mention one. What is the purpose of a constructor if it is not required to initialise the fields like the one I have mentioned below?

I also understand that a constructor without parameter is required as it is necessary during object creation when an argument is not passed (when programmer-defined constructors with parameters exists).

Is it necessary for JVM to provide a constructor which is actually not required?

 public class test {
        int a;
        public test() { 
       //this constructor is useless

    }
public static void main(String[] args)
{ 
  test ob= new test();
  System.out.println(ob.a); 
//this prints 0 which means a constructor is not required in intialising `a`
}
}

A constructor like test() makes sense if the programmer defines it since there could be other constructors which takes argument. But why is it necessary for JVM to provide one when no constructor is declared by the programmer?

I have already tested and proved that initialising a field doesn't require constructor.

Also what does the default constructor look like?

Upvotes: 1

Views: 6129

Answers (7)

Vishal
Vishal

Reputation: 1

Java's Compiler creates a Default Constructor if no other constructor is defined for the class.

But why?

The compiler's job is to chain the Subclass's constructor to the Super Class(ultimately Object class). It's not the Compiler's work to give a default constructor to your class therefore Java is doing it for you.

To do the chaining, it first checks if there is any constructor in the class, if yes it will add super() to that constructor.

If there is no constructor idefined in the class in order for the compiler to do a proper chaining, Java adds a default constructor and a call to super() into it.

Why ?

Because every class is a subclass of an Object class (directly or indirectly), it will inherit an object class, to do so the said Object class must be fully initialised. This is done by the default constructor.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533520

The problem is that while you know the default constructor doesn't do anything in this example, in future the constructor might do something even if you don't realise it is and you might not be able to re-compile everywhere the code is used reliably. So the safest, simplest thing to do is to always call a constructor which might change in the future and let the JIT optimise away the constructor if it doesn't actually do anything.


The byte code always calls a contructor, whether you provide one or not. When you compile code which uses the default constructor it cannot assume the constructor doesn't do anything useful as you can add something to it later to do something useful. e.g.

Say you change

public class Test {
    int a;

    // public Test() { //this constructor does nothing
}

to

public class Test {
    int a;
    final List<String> strings = new ArrayList<>();

    // public Test() { //this constructor does something now.
}

or

public class ServerTest {
    final List<String> strings = new ArrayList<>();
}

public class Test extends SuperTest {
   int a;
   // the default constructor has to call super();
}

The constructor now initialised the strings field. You can change this class without having to re-compile everywhere it is used and say, hey, my constructor now does something useful, you should call it now.

Upvotes: 4

user1089451
user1089451

Reputation:

  1. It is the compiler, and not JVM, who inserts a default constructor on absence.
  2. A default constructor is needed because the constructor of the base class needs to be called from a constructor.
  3. The default constructor looks like:

    public Test() {
        super();
    }
    

Upvotes: 1

ewanc
ewanc

Reputation: 1334

The reason the JVM adds a default constructor if you haven't provided one is down to inheritance. Say for example you have 3 classes (A, B & C) in which B extends A and C extends B. Now when you instantiate C it will call the constructor of C and also the constructors of B and A. If a constructor was missing in one or more of these classes then the instantiation would fail. So having the compiler automatically add a default constructor avoids error like this. You may not need to actually do any work in your constructor, but it's required for the JVM to instantiate the object.

Upvotes: 3

Miquel Perez
Miquel Perez

Reputation: 455

The default constructor is a no argument constructor called automatically by the compiler when you haven't defined any constructor. Anyway, a constructor as the one you defined, can be also called a default constructor. It basically calls the superclass' constructor making use of the super() method. So the default constructor called automatically would be something like:

public ClassName(){

super();

}

Upvotes: 0

Srinath Murugula
Srinath Murugula

Reputation: 582

Here while you are creating the object new test(), here parenthesis states the default constructor of object test.After creating the object,if you didnot give any constructor the default constructor is constructed by jvm.so after construction of your object first call goes to your default constructor.

Upvotes: 0

pleft
pleft

Reputation: 7905

The constructor(s) (both default and custom one(s)) is not only used to initialize the fields of an object but also to initialize the object itself even if it has no fields at all. Calling the constructor, JVM allocates memory for this object and creates its reference.

Upvotes: 1

Related Questions