Understanding Constructors in Java: Definition, Purpose, Types, Examples, and Usage in Object Creation

Understanding Constructors in Java: Definition, Purpose, Types, Examples, and Usage in Object Creation

1. What is a Constructor in Java?

A constructor is a special method used to initialize objects in Java. It has the same name as the class and does not have a return type.



Constructors are automatically called when an object is created.

public class Student {
    Student() {
        System.out.println("Constructor is called!");
    }

    public static void main(String[] args) {
        Student s1 = new Student(); // Constructor is automatically invoked
    }
}

2. Purpose of Constructors

  • Initialize object values when the object is created.

  • Set default or custom values for variables.

  • Improve code clarity by reducing the need to call setter methods after object creation.

3. Types of Constructors in Java

Java supports three main types of constructors:

  • Default Constructor

  • Parameterized Constructor

  • Copy Constructor (custom-defined)

Let’s understand each with examples.

4. Default Constructor

A constructor with no parameters is known as the default constructor.

public class Book {
    Book() {
        System.out.println("This is a default constructor.");
    }

    public static void main(String[] args) {
        Book b1 = new Book();
    }
}

Output:

This is a default constructor.

5. Parameterized Constructor

A constructor with parameters that allow you to initialize an object with custom values.

public class Book {
    String title;
    int pages;

    Book(String t, int p) {
        title = t;
        pages = p;
    }

    public void display() {
        System.out.println("Title: " + title + ", Pages: " + pages);
    }

    public static void main(String[] args) {
        Book b2 = new Book("Java Basics", 250);
        b2.display();
    }
}

Output:

Title: Java Basics, Pages: 250

6. Copy Constructor

Java doesn’t provide a built-in copy constructor, but you can define one to copy the values from another object.

public class Book {
    String title;

    Book(String t) {
        title = t;
    }

    // Copy constructor
    Book(Book b) {
        title = b.title;
    }

    public void display() {
        System.out.println("Title: " + title);
    }

    public static void main(String[] args) {
        Book original = new Book("Learn Java");
        Book copy = new Book(original);

        original.display();
        copy.display();
    }
}

Output:

Title: Learn Java  
Title: Learn Java

7. Constructor Overloading

Constructor overloading means defining multiple constructors 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: " + name + ", Age: " + age);
    }

    public static void main(String[] args) {
        Employee e1 = new Employee();
        Employee e2 = new Employee("Alice", 30);

        e1.display();
        e2.display();
    }
}

Output:

Name: Unknown, Age: 0  
Name: Alice, Age: 30

8. Key Rules for Constructors

  • Constructor name must match the class name.

  • Constructors do not have a return type, not even void.

  • They are automatically called when an object is created.

  • If no constructor is defined, Java provides a default one.

Conclusion

Constructors play a vital role in Java by initializing objects with default or specific values. Understanding constructor types and overloading is key to writing clean, efficient, and flexible Java programs.



This Content Sponsored by Buymote Shopping app

BuyMote E-Shopping Application is One of the Online Shopping App

Now Available on Play Store & App Store (Buymote E-Shopping)

Click Below Link and Install Application: https://buymote.shop/links/0f5993744a9213079a6b53e8

Sponsor Content: #buymote #buymoteeshopping #buymoteonline #buymoteshopping #buymoteapplication"

Previous Post Next Post