Constructors and Types in Java – Complete Detailed Tutorial
A constructor in Java is a special method used to initialize objects.
It has the same name as the class and no return type (not even void).
1. Why Use Constructors?
- Automatically called when an object is created
- Used to set initial values for object attributes
- Simplifies object initialization
2. Syntax of a Constructor
class ClassName {
// constructor
ClassName() {
// initialization code
}
}
Example:
class Student {
String name;
int age;
// Constructor
Student() {
name = "Unknown";
age = 18;
}
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student(); // Constructor called automatically
s1.display();
}
}
Output:
3. Types of Constructors
3.1 Default Constructor
- No parameters
- Provided automatically by Java if no constructor is defined
- Can also be explicitly defined
Example – Default Constructor:
class Car {
String brand;
int year;
Car() { // default constructor
brand = "Unknown";
year = 2020;
}
}
3.2 Parameterized Constructor
- Accepts parameters to initialize object with specific values
- Allows flexible initialization
Example – Parameterized Constructor:
class Car {
String brand;
int year;
Car(String b, int y) { // parameterized constructor
brand = b;
year = y;
}
void display() {
System.out.println("Brand: " + brand + ", Year: " + year);
}
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car("Toyota", 2021);
Car car2 = new Car("Honda", 2022);
car1.display();
car2.display();
}
}
Output:
Brand: Toyota, Year: 2021
Brand: Honda, Year: 2022
3.3 Copy Constructor
- Creates a new object using another object
- Copies the values of attributes
Example – Copy Constructor:
class Student {
String name;
int age;
Student(String n, int a) { // parameterized constructor
name = n;
age = a;
}
// Copy constructor
Student(Student s) {
name = s.name;
age = s.age;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("John", 20);
Student s2 = new Student(s1); // copy constructor
s1.display();
s2.display();
}
}
Output:
Name: John, Age: 20
Name: John, Age: 20
4. Key Points about Constructors
- Constructor name must match class name
- Constructor has no return type
- Automatically invoked when object is created
- Can be overloaded (multiple constructors with different parameters)
- Types → Default, Parameterized, Copy
5. Example Program – Using Multiple Constructors
class Book {
String title;
String author;
int year;
// Default constructor
Book() {
title = "Unknown";
author = "Unknown";
year = 2000;
}
// Parameterized constructor
Book(String t, String a, int y) {
title = t;
author = a;
year = y;
}
// Copy constructor
Book(Book b) {
title = b.title;
author = b.author;
year = b.year;
}
void display() {
System.out.println(title + " by " + author + ", Year: " + year);
}
}
public class Main {
public static void main(String[] args) {
Book b1 = new Book();
Book b2 = new Book("Java Basics", "Chinmaya", 2025);
Book b3 = new Book(b2);
b1.display();
b2.display();
b3.display();
}
}
Output:
Unknown by Unknown, Year: 2000
Java Basics by Chinmaya, Year: 2025
Java Basics by Chinmaya, Year: 2025
6. Summary
- Constructor → special method to initialize objects
- Types: Default, Parameterized, Copy
- Can be overloaded to provide flexibility
- Automatically invoked when object is created
- Essential for clean and efficient OOP code