Parameterized 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 a parameterized constructor changes as we create more objects.
We want to know how the work grows when we make many objects using this constructor.
Analyze the time complexity of the following code snippet.
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
// Creating multiple Person objects
for (int i = 0; i < n; i++) {
Person p = new Person("Name" + i, i);
}
This code defines a parameterized constructor and creates n Person objects using it.
- Primary operation: Creating a Person object using the constructor.
- How many times: The constructor runs once for each of the n objects in the loop.
Each new object requires the constructor to run once, so the total work grows directly with the number of objects.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 constructor calls |
| 100 | 100 constructor calls |
| 1000 | 1000 constructor calls |
Pattern observation: The work increases evenly as we create more objects.
Time Complexity: O(n)
This means the time to create n objects grows in a straight line with n.
[X] Wrong: "The constructor runs only once no matter how many objects we create."
[OK] Correct: Each object needs its own constructor call, so the constructor runs n times for n objects.
Understanding how constructors work with many objects helps you explain how your code scales in real projects.
"What if the constructor did some extra work inside a loop? How would that affect the time complexity?"
Practice
What is the main purpose of a parameterized constructor in Java?
Solution
Step 1: Understand constructor purpose
A constructor is used to initialize objects when they are created.Step 2: Identify parameterized constructor role
A parameterized constructor takes arguments to set initial values for the object's fields.Final Answer:
To initialize an object with specific values when it is created -> Option AQuick Check:
Parameterized constructor = initialize with values [OK]
- Confusing constructors with methods
- Thinking constructors delete objects
- Believing constructors don't take parameters
Which of the following is the correct syntax for a parameterized constructor in Java?
public class Car {
String model;
int year;
// Constructor here
}Solution
Step 1: Check constructor syntax
A constructor has no return type and matches the class name exactly.Step 2: Verify parameter usage
public Car(String model, int year) { this.model = model; this.year = year; } correctly uses parameters and assigns them to fields withthis.Final Answer:
public Car(String model, int year) { this.model = model; this.year = year; } -> Option BQuick Check:
Constructor syntax = no return type + class name [OK]
- Adding return type to constructor
- Not using 'this' to assign fields
- Using void or other return types
What will be the output of the following Java code?
class Book {
String title;
int pages;
Book(String title, int pages) {
this.title = title;
this.pages = pages;
}
void display() {
System.out.println(title + ": " + pages + " pages");
}
}
public class Main {
public static void main(String[] args) {
Book b = new Book("Java Basics", 250);
b.display();
}
}Solution
Step 1: Understand constructor initialization
The constructor setstitleto "Java Basics" andpagesto 250.Step 2: Analyze display method output
Thedisplay()method prints the title, colon, pages, and "pages" text.Final Answer:
Java Basics: 250 pages -> Option AQuick Check:
Constructor sets fields, display prints them [OK]
- Ignoring 'this' keyword effect
- Expecting only title or pages printed
- Assuming syntax error without reason
Identify the error in this Java class with a parameterized constructor:
public class Student {
String name;
int age;
public Student(String n, int a) {
name = n;
age = a;
}
public Student() {
name = "Unknown";
age = 0;
}
public void Student(String name, int age) {
this.name = name;
this.age = age;
}
}Solution
Step 1: Check constructor definitions
Constructors have no return type and match class name exactly.Step 2: Identify method named Student with void
The methodpublic void Student(String name, int age)is not a constructor but a method, which is confusing.Final Answer:
The method named Student with void return type is not a constructor -> Option CQuick Check:
Constructors have no return type [OK]
- Writing constructors with void return type
- Confusing methods with constructors
- Missing default constructor when needed
Given the class below, what will be the output when creating two Employee objects with different parameters and printing their details?
class Employee {
String name;
double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public void printInfo() {
System.out.println(name + " earns $" + salary);
}
}
public class Test {
public static void main(String[] args) {
Employee e1 = new Employee("Alice", 50000);
Employee e2 = new Employee("Bob", 60000);
e1.printInfo();
e2.printInfo();
}
}Solution
Step 1: Analyze object creation with parameters
Two Employee objects are created with names and salaries passed to the parameterized constructor.Step 2: Check printInfo output
Each object'sprintInfo()prints the name and salary correctly.Final Answer:
Alice earns $50000.0 Bob earns $60000.0 -> Option DQuick Check:
Parameterized constructor sets fields, printInfo shows them [OK]
- Mixing up values between objects
- Expecting errors without reason
- Assuming default constructor is needed here
