A Constructor in Java is a special member of a class that is used to initialize states of an object. In other words, a constructor is a special method used to initialize instance variables. It is one of the core concepts of object-oriented programming (OOP).
Without constructors, you cannot properly initialize states of an object in Java. A constructor is automatically called when you create an object of a class using new keyword.

Syntax of Constructor in Java
The general syntax to define a constructor in Java is:
class ClassName {
ClassName() {
// constructor body
}
}
Why Do We Need a Constructor in Java?
When we create an object using the new keyword in Java:
Student s = new Student();
Two main things happen:
- Memory is allocated for the object in the heap.
- The constructor is automatically called to initialize the object.
Constructors help in the following ways:
- Assigning Initial Values to Instance Variables.
- Allocating system resources for database connections, file streams, and network sockets.
- Validating data.
- Setting up configuration.
Example Without Constructor
class Student {
int rollNo;
String name;
}
Example With Constructor
class Student {
int rollNo;
String name;
// Two parameterized constructor.
Student(int r, String n) {
rollNo = r;
name = n;
}
}
Without initialization, instance variables would hold default values (0, null, false, etc.).
Types of Constructor in Java
There are mainly three types of constructors in Java:
- Default Constructor
- No-Argument Constructor
- Parameterized Constructor
Let’s understand each in detail with the help of important examples.
What is Default Constructor in Java
A default constructor in Java is a constructor that the Java compiler automatically creates when you do not define any constructor in a class. You do not write it explicitly. The compiler adds it during compilation.
The purpose of declaring default constructor in the program is initialize instance variables with default values. The default constructor has no parameters. It has the same name as the class. It does not have a return type.
Example: Default Constructor
class Student {
int rollNo;
String name;
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
System.out.println(s.rollNo);
System.out.println(s.name);
}
}
Output:
0
null
What Happened Internally?
When you did not define any constructor, the compiler automatically added this:
Student() {
super(); // Calls the parent class constructor.
}
You cannot see it in your code, but the compiler inserts it behind the scenes.
Default Values Assigned
When the default constructor runs, Java assigns default values to instance variables:
- int → 0
- double → 0.0
- boolean → false
- char → ‘\u0000’
- String (object reference) → null
- Any object reference → null
Important Rule
If you define any constructor manually, the compiler does NOT create the default constructor.
Example:
class Student {
Student(int id) {
System.out.println(id);
}
}
Now this will cause an error:
Student s = new Student(); // Compile-time Error
This will generate compile-time error because the compiler did not create the default constructor.
Memory Flow When You Create Object
When you write:
Student s = new Student();
Java performs these steps:
- JVM loads the class.
- the
newkeyword allocates memory in heap. - Instance variables receive default values.
- Default constructor executes.
super()constructor calls the parent class constructor.- Object reference is assigned to variable
s.
Role of super() in Default Constructor
The compiler inserts:
super();
It calls the constructor of the parent class (usually Object class). Every class in Java extends the Object class directly or indirectly.
When Does the Compiler Create It?
The compiler creates a default constructor only when:
- You do not write any constructor.
- The class is not abstract with parameterized constructor only.
When Does the Compiler Not Create It?
The compiler does NOT create a default constructor when:
- You define at least one constructor manually.
- You explicitly define a parameterized constructor.
Practical Example of Default Constructor in Java
Let us take an example based on the default constructor in Java.
class Car {
String brand;
int price;
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
System.out.println(c.brand);
System.out.println(c.price);
}
}
Output:
null
0
In this example:
- You did not define any constructor.
- The compiler inserted Car() { super(); }.
- Java assigned default values:
- brand → null
- price → 0
- The program printed null and 0.
Key Characteristics of Default Constructor
- The default constructor has no parameters.
- It has package-level access if you do not specify modifier.
- It only initializes variables with default values.
- It cannot perform custom initialization unless you write it manually.
- It always calls parent constructor.
Frequently Asked Interview Questions
1. Can we see the default constructor?
No. The compiler generates it internally.
2. Can we override the default constructor?
No. Constructors cannot be overridden.
3. Is default constructor always public?
No. It has the same access level as the class.
4. What happens if parent class has no default constructor?
You must explicitly call the parent constructor using super(parameters).




