A constructor is a special member of a Java class that is used to initialize an object when the object is created. Constructors are one of those Java features that look simple at first, but understanding how they really work will make object creation much clearer.
Whenever you write new followed by a class name, Java needs a way to prepare the newly created object. Constructors provide that initialization mechanism.
Why Do We Need Constructors?
Suppose you create a Student object like this:
Student student = new Student(); student.name = "Ananya"; student.age = 20; student.marks = 88;
The object is created first, and its values are assigned afterward. This works, but it allows the object to temporarily exist without meaningful information.
A constructor lets you initialize the object at the moment it is created.
Student student = new Student("Ananya", 20, 88);
Now the object can be created with its initial state in one clear operation.
What Is a Constructor?
A constructor is a special block declared inside a class that runs automatically when an object of that class is created.
class Student {
String name;
int age;
Student() {
name = "Unknown";
age = 0;
}
}
The constructor above is named Student, exactly like the class. When an object is created using new Student(), this constructor executes automatically.
Remember: A constructor has the same name as its class and does not have a return type—not even void.
Basic Constructor Syntax
class ClassName {
ClassName() {
// initialization code
}
}
The constructor name must match the class name exactly. If the class is named Employee, its constructor must also be named Employee.
A Simple Constructor Example
class Employee {
String name;
double salary;
Employee() {
name = "Unknown";
salary = 0.0;
}
void displayDetails() {
System.out.println(name);
System.out.println(salary);
}
}
public class Main {
public static void main(String[] args) {
Employee employee = new Employee();
employee.displayDetails();
}
}
When new Employee() executes, Java creates an Employee object and then invokes the matching constructor. The constructor assigns initial values to name and salary.
The important point is that you do not call the constructor like an ordinary method. It is automatically invoked as part of object creation.
Constructor with Parameters
Constructors become much more useful when they accept parameters. This allows every object to be initialized with different values.
class Employee {
String name;
double salary;
Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
}
Now different objects can be initialized with different data.
Employee employee1 = new Employee("Riya", 55000);
Employee employee2 = new Employee("Arjun", 62000);
The first constructor call initializes one object with Riya's information, while the second initializes another object with Arjun's information.
Why Use Constructors Instead of Setters During Creation?
Imagine an object that should never exist without a product ID and product name. If values are assigned separately after construction, another part of the program could accidentally forget one of them.
A constructor can make the required information explicit at the point where the object is created.
Product product = new Product(101, "Laptop");
The code communicates something useful to another developer: creating a Product requires an ID and a name.
Important: Constructors are particularly useful for establishing a valid initial state for an object. Good constructors make it difficult to accidentally create objects with missing or invalid essential data.
Default Constructor
The term default constructor is often used casually, but it has a specific meaning in Java.
If you do not declare any constructor in a class, the Java compiler automatically provides a no-argument constructor with default behavior.
class Student {
String name;
int age;
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
System.out.println(student.name);
System.out.println(student.age);
}
}
Because the Student class declares no constructor, Java provides a compiler-generated no-argument constructor. The instance variables receive their normal Java default values: null for the reference variable and 0 for the integer.
What Happens When You Define a Constructor?
This is a very important rule.
Once you explicitly declare a constructor, Java does not automatically provide the compiler-generated no-argument constructor.
class Student {
String name;
Student(String name) {
this.name = name;
}
}
Now this is valid:
Student student = new Student("Ananya");
But this is not valid because there is no no-argument constructor:
Student student = new Student();
If you want both forms, you must explicitly declare both constructors.
Constructor vs Method
| Constructor | Method |
|---|---|
| Used mainly to initialize objects | Used to perform an operation |
| Must have the same name as the class | Can have any valid method name |
| Cannot have a return type | Can have a return type or be void |
| Runs during object creation | Runs when explicitly invoked |
| Cannot be called like an ordinary method | Can be called through an object, class, or other valid mechanism |
Constructor Initialization Flow
Consider this statement:
Employee employee = new Employee("Riya", 55000);
Conceptually, the important sequence is:
- Java evaluates the new expression.
- Memory is allocated for the new object.
- The object's instance fields receive their initial values.
- The appropriate constructor is invoked.
- The constructor initializes the object's state.
- The resulting object reference is assigned to the reference variable.
You do not normally need to manually manage these steps, but understanding the sequence becomes valuable when you study inheritance, initialization blocks, and object lifecycle behavior.
Using this Inside a Constructor
A common constructor pattern occurs when parameter names are the same as instance variable names.
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Here, this.name refers to the object's instance variable, while name refers to the constructor parameter. The same applies to age.
The this keyword will be explored in detail in a later chapter, but this constructor pattern is important enough to recognize early.
Multiple Constructors
A class can have more than one constructor, provided their parameter lists are different. This is known as constructor overloading, which will be covered in the next chapter.
class Product {
int id;
String name;
Product() {
id = 0;
name = "Unknown";
}
Product(int id, String name) {
this.id = id;
this.name = name;
}
}
Now the class supports both a no-argument construction and a construction that accepts product information.
Common Beginner Mistakes
- Adding void before a constructor name.
- Giving a constructor a name different from the class name.
- Assuming Java always provides a no-argument constructor even after another constructor has been declared.
- Trying to invoke a constructor like an ordinary method after the object has already been created.
- Confusing constructor parameters with instance variables.
Best Practices
- Use constructors to establish meaningful initial object state.
- Require essential information through constructor parameters when appropriate.
- Keep constructor logic focused on initialization rather than performing unrelated business operations.
- Validate important input when the object's validity depends on it.
- Use constructor overloading carefully so that object creation remains easy to understand.
Interview Insights
A common interview question is: “What is a constructor in Java?”
A strong answer is: A constructor is a special class member used to initialize an object when it is created. It has the same name as the class, has no return type, and is invoked as part of object creation.
Another frequently tested question is: “Does Java provide a default constructor if you define another constructor?” The answer is no. The compiler-generated no-argument constructor is provided only when the class contains no explicitly declared constructor.
Quick Learning Check
Before moving to constructor overloading, make sure you can answer these questions:
- What is the purpose of a constructor?
- Why does a constructor not have a return type?
- When does a constructor execute?
- What happens if a class declares no constructor?
- What happens to the compiler-provided no-argument constructor when you explicitly declare another constructor?
- Why are parameterized constructors useful?
Final Takeaway
Constructors provide Java's standard mechanism for initializing objects during creation. They can establish default state, accept required information, and help ensure that objects begin their lives in a meaningful configuration. Once you understand that constructors are tied directly to object creation and that explicitly declaring one affects the compiler-provided no-argument constructor, you are ready to explore constructor overloading and more advanced initialization patterns.
