Java Constructors Explained Simply
When you create an object using the new keyword, Java automatically runs a special block of code. That block is called a constructor.
It prepares the object by setting up its initial state. This can include assigning values to variables, printing a message, or any setup logic you want.
1. What is a Constructor?
- It has the same name as the class.
- No return type (not even void).
- It automatically runs when an object is created.
public class Student {
Student() {
System.out.println("Constructor called");
}
public static void main(String[] args) {
Student s = new Student();
}
}
Output:
Constructor called
2. Types of Constructors
Java provides three main types of constructors:
- Default Constructor
- No-Argument Constructor
- Parameterized Constructor
2.1 Default Constructor
If you don't write a constructor, Java gives you one automatically. That's called the default constructor. You can also define one yourself.
public class Bike {
Bike() {
System.out.println("Bike is ready");
}
public static void main(String[] args) {
Bike b = new Bike();
}
}
Output:
Bike is ready
2.2 No-Argument Constructor
A no-argument constructor is a constructor that takes no parameters. It can be used to initialize default values or simply confirm object creation.
public class Book {
Book() {
System.out.println("Book object created");
}
public static void main(String[] args) {
Book b = new Book();
}
}
Output:
Book object created
2.3 Parameterized Constructor
This constructor accepts arguments when you create an object. That means you can pass data during object creation.
public class Car {
String model;
int year;
Car(String m, int y) {
model = m;
year = y;
}
public void show() {
System.out.println(model + " - " + year);
}
public static void main(String[] args) {
Car c = new Car("Tesla", 2025);
c.show();
}
}
Output:
Tesla - 2025
3. Constructor Overloading
Just like method overloading, you can define multiple constructors in the same class with different parameter lists.
public class Employee {
String name;
int age;
Employee() {
name = "Unknown";
age = 0;
}
Employee(String n, int a) {
name = n;
age = a;
}
public void display() {
System.out.println(name + " - " + age);
}
public static void main(String[] args) {
Employee e1 = new Employee();
Employee e2 = new Employee("Alice", 30);
e1.display();
e2.display();
}
}
Output:
Unknown - 0
Alice - 30
4. Common Mistakes to Avoid
- Adding a return type to a constructor
Student void() { } // Wrong – this becomes a method
Student() { } // Correct constructor
- Misspelling the constructor name
public class Student {
Stuent() { } // Wrong – typo in constructor name
}
public class Student {
Student() { } // Correct
}
- Forgetting to assign values in parameterized constructor
Person(String n) {
// name = n; // Forgot to assign
}
Person(String n) {
name = n; // Correct
}
5. Why Use Constructors?
Without constructor:
Student s = new Student();
s.name = "Ravi";
s.age = 22;
With constructor:
Student s = new Student("Ravi", 22);
Cleaner, safer, and less error-prone
Makes sure the object is initialized properly from the beginning
Official Java Documentation: Java Constructors