Introduction
No-argument constructor (also called default constructor) is a constructor that takes no parameters.
Two Types
1. Java-Provided Default Constructor
If you don’t define ANY constructor, Java automatically provides one.
2. User-Defined No-argument Constructor
You explicitly create a constructor with no parameters.
Java-Provided Default Constructor
When Provided:
- Only when NO constructors are defined
- Automatically created by compiler
- Does nothing (empty body)
- Has same access modifier as class
Example:
// No constructor defined
class Student {
String name;
int rollNo;
}
// Java automatically provides:
// public Student() { }
public class Main {
public static void main(String[] args) {
Student s = new Student(); // Uses default constructor
s.name = "John";
s.rollNo = 101;
}
}
When Default Constructor is NOT Provided
class Student {
String name;
int rollNo;
// Parameterized constructor defined
Student(String name, int rollNo) {
this.name = name;
this.rollNo = rollNo;
}
}
public class Main {
public static void main(String[] args) {
// Student s = new Student(); // ❌ Error: no default constructor
Student s = new Student("John", 101); // ✓ OK
}
}
Key Point: Once you define ANY constructor, Java does NOT provide default constructor.
User-Defined No-argument Constructor
class Student {
String name;
int rollNo;
// User-defined no-argument constructor
Student() {
name = "Unknown";
rollNo = 0;
System.out.println("No-argument constructor called");
}
void display() {
System.out.println("Name: " + name);
System.out.println("Roll No: " + rollNo);
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.display();
}
}
Output:
No-argument constructor called
Name: Unknown
Roll No: 0
With Other Constructors
class Student {
String name;
int rollNo;
// No-argument constructor
Student() {
this.name = "Unknown";
this.rollNo = 0;
}
// Parameterized constructor
Student(String name, int rollNo) {
this.name = name;
this.rollNo = rollNo;
}
void display() {
System.out.println(name + ", " + rollNo);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student(); // No-argument
Student s2 = new Student("John", 101); // Parameterized
s1.display(); // Unknown, 0
s2.display(); // John, 101
}
}
Using this() to Call Another Constructor
class Student {
String name;
int rollNo;
double marks;
// No-argument constructor calls parameterized
Student() {
this("Unknown", 0, 0.0);
}
Student(String name, int rollNo, double marks) {
this.name = name;
this.rollNo = rollNo;
this.marks = marks;
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
System.out.println(s.name); // Unknown
}
}
Complete Example
class BankAccount {
private String accountNumber;
private String holderName;
private double balance;
// No-argument constructor
public BankAccount() {
this.accountNumber = "0000000000";
this.holderName = "Unknown";
this.balance = 0.0;
System.out.println("Account created with default values");
}
// Parameterized constructor
public BankAccount(String accountNumber, String holderName, double balance) {
this.accountNumber = accountNumber;
this.holderName = holderName;
this.balance = balance;
System.out.println("Account created with given values");
}
public void display() {
System.out.println("Account: " + accountNumber);
System.out.println("Holder: " + holderName);
System.out.println("Balance: " + balance);
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
BankAccount acc1 = new BankAccount();
BankAccount acc2 = new BankAccount("1234567890", "John", 5000);
acc1.display();
acc2.display();
}
}
When to Use
Use No-argument Constructor:
- Default values needed
- Flexibility in object creation
- Frameworks require it (JavaBeans)
- Arrays of objects
Example: Array of Objects
class Student {
String name;
int rollNo;
// No-argument constructor required
Student() {
name = "Unknown";
rollNo = 0;
}
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[3];
// Create objects using no-argument constructor
for (int i = 0; i < students.length; i++) {
students[i] = new Student();
}
}
}
Default vs No-argument Constructor
| Feature | Default Constructor | No-argument Constructor |
|---|---|---|
| Provider | Java compiler | Programmer |
| When | No constructor defined | Explicitly defined |
| Body | Empty | Can have code |
| Access | Same as class | Can be any |
| Removed | When any constructor defined | Manual removal needed |
Common Mistake
class Student {
String name;
int rollNo;
// Only parameterized constructor
Student(String name, int rollNo) {
this.name = name;
this.rollNo = rollNo;
}
}
public class Main {
public static void main(String[] args) {
// Student s = new Student(); // ❌ Error!
// Default constructor NOT available
Student s = new Student("John", 101); // ✓ OK
}
}
Solution: Add no-argument constructor explicitly.
class Student {
String name;
int rollNo;
// Add no-argument constructor
Student() {
this("Unknown", 0);
}
Student(String name, int rollNo) {
this.name = name;
this.rollNo = rollNo;
}
}
Best Practices
1. Always Provide No-argument Constructor
class Good {
String name;
// Good: Both constructors
Good() {
name = "Default";
}
Good(String name) {
this.name = name;
}
}
2. Use Constructor Chaining
class Better {
String name;
Better() {
this("Default"); // Call parameterized
}
Better(String name) {
this.name = name;
}
}
Private No-argument Constructor
Used to prevent object creation (Singleton pattern).
class Singleton {
private static Singleton instance;
// Private no-argument constructor
private Singleton() {
System.out.println("Singleton created");
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
public class Main {
public static void main(String[] args) {
// Singleton s = new Singleton(); // ❌ Error: private
Singleton s1 = Singleton.getInstance(); // ✓ OK
Singleton s2 = Singleton.getInstance(); // Same object
}
}
Quick Reference
// Java-provided default (if no constructor)
class A {
int x;
} // Java adds: A() { }
// User-defined no-argument
class B {
int x;
B() { x = 10; } // Explicit
}
// With parameterized
class C {
int x;
C() { x = 0; } // No-argument
C(int x) { this.x = x; } // Parameterized
}
// Using this()
class D {
int x;
D() { this(10); } // Call parameterized
D(int x) { this.x = x; }
}
Exam Tips
Remember:
- Default constructor provided only if NO constructors defined
- No-argument = zero parameters
- Once ANY constructor defined, default NOT provided
- Can be explicitly defined
- Useful for default values
- Required for arrays of objects
- Can use this() to call other constructors
- Can be private (Singleton)
- Same name as class
- No return type
Common Questions:
- What is default constructor?
- When is it provided?
- Difference between default and no-argument?
- What happens if you define parameterized constructor?
- Why provide no-argument constructor?
- Can it be private?
- Uses of no-argument constructor?