Introduction
The Java compiler does not always provide a default constructor. It only provides a default constructor when the programmer does not provide any constructor in the class. This blog will first introduce the different types of constructors and then prove that the compiler does not always provide a default constructor.
1. Types of Constructors
Generally speaking, there are three types of constructors: the default constructor, no-parameter constructor, and constructor with parameters.
Default constructor: A constructor without parameters, which is created by the Java compiler during the compiling phase.
No-parameter constructor: A constructor without parameters, which is created by the programmers when they write the source code(.java file).
Constructor with parameters: A constructor that have some parameters, which is created by the programmers when they write the source code(.java file).
2. The Proof Process
2.1. The process of compiling and interpreting a Java program
The process of compiling and interpreting a Java source code is: first, a programmer writes a .java file; second, the compiler will compile it to a .class file; finally, the interpreter(JVM, Java Virtue Machine) will interpret it.
2.2. The proof steps
The Java compiler would add the default constructor when compiling the .java file into the .class file. Hence, the .class file should have a constructor created by the compiler if it did provide. Therefore, we can check the .class file to see whether the compiler would give a default constructor if the programmers do not offer any constructor. For the .class file, we can use the javap command to disassemble it, and the outputs will show us what it includes. Simply, the process is:
Next, I will use two programs to prove: a .java file without any constructor given by the programmer and a .java file with a constructor provided by the programmer.
2.3. The proof examples
2.3.1. Example of a .java file without constructor given by the programmer
The following DocFooter.java is the source code without any constructor declaring by the programmer:
1 | import java.awt.*; |
Now, we can use the java command to compile the DocFooter.java, and the compiler will generate a .class file, like this:
1 | C:\Users\AI\Desktop>java DocFooter.java |
After getting the .class file, we can use the javap command to disassemble it. And the outputs will show us what it includes:
1 | C:\Users\AI\Desktop>javap DocFooter.class |
As we can see, there are no constructors in the DocFooter.java file, but in the disassemble outputs, there is a constructor: DocFooter(), which illustrates that the compiler would provide a default constructor if the programmer did not offer constructors to the class.
2.3.2. Example of a .java file with a constructor provided by the programmer
Now, I will add a constructor in the above DocFooter.java to see whether the compiler will add a default constructor. The following DocFooter.java is the source code with a constructor declaring by the programmer:
1 | import java.awt.*; |
Now, we can use the java command to compile the DocFooter.java, and the compiler will generate a .class file, like this:
1 | C:\Users\AI\Desktop>java DocFooter.java |
After getting the .class file, we can use the javap command to disassemble it. And the outputs will show us what it includes:
1 | C:\Users\AI\Desktop>javap DocFooter.class |
As we can see, there is no default constructor in the outputs, and it only has the constructor: DocFooter(java.lang.String, java.lang.String), which the programmer provides. Hence, the compiler does not provide a default constructor if the programmer provides constructors in the source codes.
3. In Conclusion
Based on the above examples and at a compiler level, we prove that the Java compiler does not always provide a default constructor. It only provides a default constructor when the programmer does not provide any constructor in the souce code.
References
1.Downey, A.B., & Mayfield, C. (2019). Think Java (2nd ed.). Green Tea Press.
https://open.umn.edu/opentextbooks/formats/331
2.ORACLE. (n.d.). Providing Constructors for Your Classes.
https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
3.ORACLE. (n.d.). javap - The Java Class File Disassembler.
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html