Default constructor in Java - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's see how the time it takes to run code with a default constructor changes as we create more objects.
We want to know how the work grows when making many objects using the default constructor.
Analyze the time complexity of the following code snippet.
public class Box {
public Box() {
// Default constructor does nothing special
}
}
public class Main {
public static void main(String[] args) {
int n = 1000;
for (int i = 0; i < n; i++) {
Box b = new Box();
}
}
}
This code creates n Box objects using the default constructor inside a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new Box object by calling the default constructor.
- How many times: Exactly n times, once for each loop iteration.
Each time we increase n, we create more Box objects, so the work grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 object creations |
| 100 | 100 object creations |
| 1000 | 1000 object creations |
Pattern observation: Doubling n doubles the number of objects created and the work done.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of objects created.
[X] Wrong: "Creating objects with a default constructor takes constant time no matter how many objects we make."
[OK] Correct: Each object creation takes some time, so making more objects means more total time, growing with n.
Understanding how object creation scales helps you reason about program speed and resource use in real projects.
What if the constructor did some work like initializing a large array? How would the time complexity change?
Practice
Solution
Step 1: Understand what a constructor is
A constructor is a special method used to create objects of a class.Step 2: Identify the default constructor
If no constructor is written, Java automatically provides a constructor with no parameters called the default constructor.Final Answer:
A constructor with no parameters that Java provides automatically if none is written. -> Option BQuick Check:
Default constructor = automatic no-parameter constructor [OK]
- Thinking default constructor must be written manually
- Confusing default constructor with methods returning default values
- Believing default constructor runs at program end
Solution
Step 1: Recall constructor syntax
A constructor has the same name as the class and no return type.Step 2: Check each option
public ClassName() { } matches the class name and has no return type, so it's correct syntax.Final Answer:
public ClassName() { } -> Option CQuick Check:
Constructor syntax = class name + no return type [OK]
- Adding void return type to constructor
- Using wrong parameter list syntax
- Using lowercase class name in constructor
class Car {
String model;
}
public class Test {
public static void main(String[] args) {
Car c = new Car();
System.out.println(c.model);
}
}Solution
Step 1: Understand default constructor usage
No constructor is defined, so Java provides a default constructor that sets no initial values.Step 2: Check default value of uninitialized String
Instance variable 'model' is a String and defaults to null if not set.Final Answer:
null -> Option AQuick Check:
Uninitialized String = null by default [OK]
- Expecting empty string instead of null
- Thinking default constructor sets values automatically
- Assuming compilation or runtime error
public class Book {
String title;
public Book() {
title = "Java Basics";
}
public Book(String title) {
title = title;
}
}Solution
Step 1: Analyze the second constructor
It assigns parameter 'title' to itself, not to the instance variable.Step 2: Understand correct assignment
Use 'this.title = title;' to assign parameter to instance variable.Final Answer:
The second constructor does not set the instance variable correctly. -> Option AQuick Check:
Use 'this' to assign constructor parameters to fields [OK]
- Assigning parameter to itself instead of instance variable
- Missing default constructor (actually present)
- Adding return types to constructors
Person that sets the name to "Unknown" by default if no name is given. Which constructor code correctly implements this using a default constructor?Solution
Step 1: Check default constructor sets default value
public class Person { String name; public Person() { name = "Unknown"; } public Person(String name) { this.name = name; } }'s default constructor sets name to "Unknown" correctly.Step 2: Verify parameterized constructor sets name properly
public class Person { String name; public Person() { name = "Unknown"; } public Person(String name) { this.name = name; } } uses 'this.name = name;' to assign parameter to instance variable.Final Answer:
public class Person { String name; public Person() { name = "Unknown"; } public Person(String name) { this.name = name; } } -> Option DQuick Check:
Default constructor sets default value, parameterized sets given value [OK]
- Assigning parameter to itself without 'this.'
- Not setting default value in default constructor
- Missing default constructor entirely
