Default Constructor provided by Compiler


When we do not explicitly provide a constructor to our class, the compiler provides a default constructor which initialises all the instance variables to their default values. The default constructor provided by the compiler is different from the no argument or default constructor which we provide ourselves. In the constructor that we provide, we are able to set the variables to any default values that we wish to do so which may be 0 ,-1 or some other different value, but this is not the case with the default constructor provided by the compiler. It sets variables of different data types to the default values given by the following table:
Data Type Variable Name
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0
boolean false
char 0
<All reference types> Null
To verify this fact, remove all the constructors from the Student class, create an object of that class and print the information stored in the variables.
public class Student {

Private int rollNumber;
   private String name;
   private int marks1;
   private int marks2;
   private int marks3;

Public void printDetails() {
      System.out.println(“Roll Number: ” + rollNumber);
      System.out.println(“Name: ” + name);
      System.out.println(“Marks in first subject: ” + marks1);
      System.out.println(“Marks in second subject: ” + marks2);
      System.out.println(“Marks in second subject: ” + marks3);
      }
   }

Public class StudentTest {

Public static void main ( String[] args ) {
      Student s=new Student();
      s.printDetails();
   }
   }

We get the following output:
Roll Number: 0
Name: null
Marks in first subject: 0
Marks in second subject: 0
Marks in second subject: 0
Average: 0.0
As you can see, all the variables were set to 0 or their equivalent. String which is also a reference type was set to null. (All classes are reference types). When a variable of a class type is set to null, it means that the variable wasn’t initialized.
When we provide atleast one constructor to our class, then the compiler no more provides a constructor. It doesn’t matter whether we are providing a no argument constructor or a parameterised constructor. The following code generates compilation errors since the class Student has no default constructor and we are trying to create an object using a default constructor.
class Student {

Public Student ( int m ) {
   }
}

Class StudentTest {

Public static void main ( String args[] ) {
      Student s=new Student(); // Incorrect
   }
}

Also, if we do not initialise any variables in the constructors provided by us, then they too are set to their default values. This is not the case with local variables (those defined in methods).
class Student {

Int m;

Public Student () {
   }
}

Class StudentTest {

Public static void main ( String args[] ) {
      Student s=new Student();
      System.out.println(s.m);
   }
}

The above program prints 0 in the output. This is because any variables that are uninitialized in the constructor are initialized with their default values. However, as the following program shows, this default initialisation does not occur with other types of variables.
public void print() {
   int x;
   System.out.println(x); // A compilation error would be generated saying that x was not initialized
}
Author: , 0000-00-00