OOP · Core Java OOP Features
Constructor
Independent object lifecycles.
- Independent object lifecycles.
- Commonly used in layered architectures.
Advantages
- Promotes loose coupling.
- Improves code flexibility.
- Simplifies testing.
- Enhances maintainability.
Real-World Problem
Imagine you are creating a Student Management System. Whenever a new student joins the college, you need to initialize details such as:
- Student ID
- Name
- Department
If you don't initialize these values when the object is created, the object may contain incorrect or default data. To ensure every object starts with the required information, Java provides Constructors Why Do We Need Constructors? Whenever an object is created, it often requires some initial values before it can be used.
Instead of assigning values manually every time, constructors automatically initialize the object during creation. This reduces repetitive code, improves readability, and ensures that every object starts in a valid state. What is a Constructor?
A Constructor is a special member of a class that is automatically executed when an object is created. Its primary purpose is to initialize the object's data. A constructor has the same name as the class and does not have a return type.
Characteristics of Constructors

- Has the same name as the class.
- Does not have a return type.
- Executes automatically when an object is created.
- Used to initialize objects.
- Can be overloaded.
Types of Constructors
Real-World Problem
Imagine a Student Management System. Different situations require creating student objects in different ways.
- Sometimes, we don't have any student information yet.
- Sometimes, we only know the student's name.
- Sometimes, we know all the student details.
Using only one constructor cannot handle all these situations efficiently. To provide flexibility during object creation, Java supports different types of constructors.

Why Do We Need Different Types of Constructors? Not every object is created with the same amount of information. Some objects require default values, while others need user-provided values. Different constructors allow us to initialize objects based on the available data, making our programs more flexible and reusable.
What are the Types of Constructors? Java provides different types of constructors to initialize objects according to different requirements. The three commonly used constructor types are:
- Default Constructor
- No-Argument Constructor
- Parameterized Constructor
Types of Constructors
Default Constructor
No-Argument Constructor
Parameterized Constructor
. Default Constructor
Used when no constructor is written in the class. Java automatically creates it and initializes object variables with default values.
2. No-Argument Constructor
A constructor explicitly written by the programmer without any parameters. It allows custom initialization instead of Java's default initialization.
3. Parameterized Constructor
A constructor that accepts parameters to initialize an object with user-provided values during object creation.

A constructor initializes a new object so it starts life in a valid state.
public class Point {
private final int x, y;
// Runs on 'new' — guarantees every Point has coordinates
public Point(int x, int y) {
this.x = x; // 'this' distinguishes field from parameter
this.y = y;
}
public String toString() { return "(" + x + ", " + y + ")"; }
public static void main(String[] args) {
System.out.println(new Point(3, 4)); // (3, 4)
}
}