Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who provides the default constructor in Java? Compiler or JVM?

Is the constructor added at run-time or compile time?(I guess it is compile time). I need some in depth explanation please, at JVM architecture level.

I read various articles.. some says compiler.. and others say JVM. I want to be very much sure(proofs will help a lot).

Sorry if the question is stupid(am still digesting the terminologies)!!!

Thanks in advance.

like image 901
Dheemanth Bhat Avatar asked May 02 '17 18:05

Dheemanth Bhat


People also ask

Does compiler create default constructor Java?

Java compiler automatically creates a default constructor (Constructor with no arguments) in case no constructor is present in the java class.

Who provides the default constructor?

The default constructor in Java initializes the data members of the class to their default values such as 0 for int, 0.0 for double etc. This constructor is implemented by default by the Java compiler if there is no explicit constructor implemented by the user for the class.

Does compiler create default constructor?

In C++, the compiler creates a default constructor if we don't define our own constructor. In C++, compiler created default constructor has an empty body, i.e., it doesn't assign default values to data members. However, in Java default constructors assign default values.


2 Answers

From the Java Tutorial from Oracle: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.

like image 52
Simon Martinelli Avatar answered Nov 15 '22 14:11

Simon Martinelli


For a formal reference, the nature of the default constructor is explained in both:

  • The Java Language Specification (JLS), section 8.8.9 Default Constructor
  • The Java Virtual Machine Specification, section 2.12 Constructors.

Alternatively, if you're using Oracle JDK or OpenJDK, you could easily demonstrate this behavior to verify that it is the compiler the one that does all the magic.

All you need to do is to use the Java decompiler tool that comes with your JDK to see what bytecodes are being generated in your class files.

You should see an executable named javap under $JDK_HOME/bin/

If you had a simple file like Demo.java containing just one class, e.g.

public class Demo {}

And you compile it using the command javac Demo.java, and then you run the decompiler using javap -c Demo, the output should say something like:

Compiled from "Demo.java"
public class Demo {
  public Demo();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return
}

Which demonstrates that it is the compiler which adds the default constructor, since it was not in your source code but it is indeed in your compiled class file.

You will also notice that the constructor access level matches that of the class for which it was generated. So, if you do

public class Demo { 
  protected static class Other {}
}

And you compile that, and then do a javap -c Demo.Other you get

public class Demo$Other {
  protected Demo$Other();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return
}

Once more, demonstrating that the compiler adds a default constructor matching the accessibility of the class for which it was generated as the specifications above say it should do.

like image 23
Edwin Dalorzo Avatar answered Nov 15 '22 14:11

Edwin Dalorzo