The default constructor
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)You write a small class with no constructor, and new still works. Who built the object, and what values did its fields get?
Java adds one when you write none
A default constructor is the constructor the compiler adds automatically when a class has no constructor of its own. It has no parameters and an empty body, so every field keeps its default value.
In the Classes and Objects module, new Student() worked though Student had no constructor. Every new needs one, so javac quietly added the default constructor, Student() { }, to the compiled class.
Example 1 · Java's default constructor at work
class Wallet { String owner; int balance; double cashback; boolean kycDone; int[] lastPayments;}
public class DefaultValuesDemo { public static void main(String[] args) { Wallet w = new Wallet(); System.out.println(w.owner); System.out.println(w.balance); System.out.println(w.cashback); System.out.println(w.kycDone); System.out.println(w.lastPayments); }}null 0 0.0 false null
What this code does
Wallethas no constructor, sojavacaddedWallet() { }andnew Wallet()compiles.- That constructor sets nothing, so each field prints its default. The array field is
nulltoo: no array exists yet.
The added constructor sets nothing, so each field shows the value new gave it: 0 for whole numbers, 0.0 for decimals, false for boolean, and null for String, arrays and other objects. A char field starts as \u0000, the character with code 0.
Field initializers: better starting values
A field initializer is a value written on the field's own line, like int seats = 40;. Every new object starts with that value, and it is set before the constructor body runs.
A new bus has 40 seats, not 0. Write the value right on the field, int seats = 40;: this is a field initializer, and every new object starts with that value, even with the default constructor.
Example 2 · Field initializers give better starting values
class AppSettings { String language = "English"; int fontSize = 16; boolean darkMode;}
public class SettingsDemo { public static void main(String[] args) { AppSettings priya = new AppSettings(); AppSettings rohan = new AppSettings(); rohan.fontSize = 20; rohan.darkMode = true;
System.out.println("Priya: " + priya.language + ", " + priya.fontSize + ", " + priya.darkMode); System.out.println("Rohan: " + rohan.language + ", " + rohan.fontSize + ", " + rohan.darkMode); }}Priya: English, 16, false Rohan: English, 20, true
What this code does
= "English"and= 16are field initializers. Both objects start with them, still using the default constructor.- Rohan changes his own copies afterwards; Priya keeps the starting values.
Each object gets its own copy of the starting value, so changing rohan.fontSize does not touch priya. A field without an initializer still gets its default, like darkMode here.
Initializers run before the constructor body
A field can have an initializer, and the constructor can change the same field. Java runs the field initializers first, at new, and the constructor body after them, so the constructor has the last word.
Example 3 · The initializer runs first, the constructor last
class Score { int runs = 10;
Score() { System.out.println("Constructor sees runs = " + runs); runs = runs + 5; }}
public class InitOrderDemo { public static void main(String[] args) { Score s = new Score(); System.out.println("After new: " + s.runs); }}Constructor sees runs = 10 After new: 15
What this code does
- When the constructor body starts,
runsis already10, not0: the field initializer ran before it. - The body then changes it to
15, and that is the value the object keeps.
So the full order at new is: defaults, then field initializers, then the constructor body. Call one constructor from another (opens in a new tab) steps through this order with several constructors.
Write any constructor and the default one disappears
Someone adds Student(String studentName) to a class, and old code with new Student() stops compiling. Java adds the default constructor only when the class has no constructor at all.
Example 4 · Writing a constructor removes the default one
class Student { String name;
Student(String studentName) { name = studentName; }}
public class NoDefaultDemo { public static void main(String[] args) { Student s = new Student(); System.out.println(s.name); }}NoDefaultDemo.java:11: error: constructor Student in class Student cannot be applied to given types;
Student s = new Student();
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
1 errorWhat this code does
- The class has a constructor, so
javacdid not add the empty one. The only way to make aStudentis with aString. required: Stringandfound: no argumentssay exactly what the constructor wants and what the call gave.
There are two fixes: pass the value the constructor needs, or write your own constructor with no parameters next to it. A class can have both; Constructor overloading (opens in a new tab) shows more.
Fix the error
Fix the missing-constructor error
Read the error, then pick the line that fixes it.
Round 1 of 4
This should create a ticket for a guest.
Error
error: constructor Ticket in class Ticket cannot be applied to given types; required: String found: no arguments reason: actual and formal argument lists differ in length
Your own no-argument constructor
A no-argument constructor is a constructor with no parameters, like Student() { ... }. The default constructor is one, but you can also write your own, which can set starting values.
Every new recharge plan should start as Basic, Rs. 199 for 28 days. A no-argument constructor you write yourself, RechargePlan() { ... }, can set such values or print a message, unlike the empty one Java adds.
Example 5 · Your own no-argument constructor
class RechargePlan { String name; int price; int days;
RechargePlan() { name = "Basic"; price = 199; days = 28; System.out.println("Plan created"); }}
public class PlanDemo { public static void main(String[] args) { RechargePlan p1 = new RechargePlan(); RechargePlan p2 = new RechargePlan(); p2.price = 249; System.out.println(p1.name + ": Rs. " + p1.price + " for " + p1.days + " days"); System.out.println(p2.name + ": Rs. " + p2.price + " for " + p2.days + " days"); }}Plan created Plan created Basic: Rs. 199 for 28 days Basic: Rs. 249 for 28 days
What this code does
RechargePlan()is a no-argument constructor you wrote. It sets useful values and prints a message for each object.- Both objects start the same; changing
p2.priceafterwards does not touchp1.
Strictly, "default constructor" means only the one javac adds. One you write with empty brackets is a no-argument (no-arg) constructor, even though many people loosely call it default too.
Next step
Check your understanding
12 quick questions