Java Constructors and Their Types – Complete Guide with Examples - Textnotes

Java Constructors and Their Types – Complete Guide with Examples


Learn how to use constructors in Java, including default, parameterized, and copy constructors, with examples to initialize objects effectively in OOP.

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?

  1. Automatically called when an object is created
  2. Used to set initial values for object attributes
  3. 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:


Name: Unknown
Age: 18

3. Types of Constructors

3.1 Default Constructor

  1. No parameters
  2. Provided automatically by Java if no constructor is defined
  3. 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

  1. Accepts parameters to initialize object with specific values
  2. 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

  1. Creates a new object using another object
  2. 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

  1. Constructor name must match class name
  2. Constructor has no return type
  3. Automatically invoked when object is created
  4. Can be overloaded (multiple constructors with different parameters)
  5. 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

  1. Constructor → special method to initialize objects
  2. Types: Default, Parameterized, Copy
  3. Can be overloaded to provide flexibility
  4. Automatically invoked when object is created
  5. Essential for clean and efficient OOP code