codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Subjects
🧩

OOP Concepts

23 chapters

1Procedural vs Object-Oriented2Classes, Objects, & Instantiation3Constructors & Destructors4Static Members & Methods5Encapsulation & Access Modifiers6Data Abstraction7Inheritance Types (Single, Multiple)8Compile-Time Polymorphism (Overloading)9Polymorphism & Interfaces10Run-Time Polymorphism (Overriding)11Virtual Functions & V-Tables12Interfaces & Abstract Classes13Generic Programming (Templates & Generics)14Exception Handling in OOP15SOLID Design Principles16Composition over Inheritance17Coupling & Cohesion18UML Diagrams Basics19Creational Patterns (Singleton, Factory)20Structural Patterns (Adapter, Decorator)21Behavioral Patterns (Observer, Strategy)22MVC Architecture Pattern23Object Serialization & Cloning
SubjectsOOP Concepts

Constructors & Destructors

Updated 2026-04-23
2 min read

Constructors & Destructors

When you create a new object with new Car(), you often want the object to be immediately initialized with valid data. You don't want to rely on the programmer remembering to manually set every field after creation. Constructors solve this problem.

1. What is a Constructor?

A Constructor is a special method that is automatically called at the moment an object is instantiated. Its primary purpose is to initialize the object's attributes.

Rules for Constructors:

  • The constructor name must be identical to the class name.
  • Constructors have no return type (not even void).
  • They are called automatically by the new keyword; you never call them directly.
class Student {
    String name;
    int age;

    // Constructor
    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

// Usage
Student s = new Student("Alice", 20);
// name and age are immediately initialized. No manual setup needed.

2. Types of Constructors

Default Constructor

If you do not write any constructor in your class, the compiler automatically provides a no-argument Default Constructor that initializes all attributes to their default values (0, null, false).

However, the moment you write any custom constructor, the compiler stops providing the default one. If you still want a no-argument constructor, you must explicitly write one yourself.

Parameterized Constructor

A constructor that accepts arguments to initialize the object with specific values.

Student(String name, int age) {
    this.name = name;
    this.age = age;
}

Copy Constructor

A constructor that creates a new object as a copy of an existing object. While C++ has a formal copy constructor mechanism, Java does not have one built-in; you must implement it manually.

// Copy constructor in Java
Student(Student other) {
    this.name = other.name;
    this.age = other.age;
}

3. Constructor Overloading

A class can have multiple constructors with different parameter lists. This is called Constructor Overloading. The compiler determines which constructor to call based on the number and types of arguments passed.

class Student {
    String name;
    int age;

    Student() {
        this.name = "Unknown";
        this.age = 0;
    }

    Student(String name) {
        this.name = name;
        this.age = 18; // default age
    }

    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

4. Destructors

A Destructor is a special method called when an object is being destroyed to release resources (like closing file handles, freeing network connections, or deallocating memory).

In C++

C++ provides explicit destructors using the tilde (~) syntax. They are called automatically when an object goes out of scope or is explicitly deleted.

class FileHandler {
public:
    ~FileHandler() {
        // Close the file handle
        fclose(this->filePtr);
    }
};

In Java (Garbage Collection)

Java does not have destructors. Instead, the Garbage Collector (GC) automatically reclaims memory from objects that are no longer referenced by any variable. The GC runs in the background as a daemon thread, and the programmer has no direct control over when (or if) an object's memory will be freed.

Java previously had a finalize() method, but it was officially deprecated due to unreliable behavior and severe performance penalties.



PreviousClasses, Objects, & InstantiationNextStatic Members & Methods

Recommended Gear

Classes, Objects, & InstantiationStatic Members & Methods