Constructors in Java
×


Constructors in Java

138

Introduction to Constructors in Java

In Java, constructors are special methods that are used to initialize objects. When an object is created from a class, the constructor is automatically called to set up the object with initial values or to run specific startup logic. Understanding Constructors in Java is fundamental to mastering object-oriented programming.

What is a Constructor?

A constructor is a block of code similar to a method, but with no return type and the same name as the class. Its main purpose is to initialize new objects. Unlike regular methods, constructors are invoked automatically when an object is created using the new keyword.


class Car {
    Car() {
        System.out.println("Car object created");
    }
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car(); // Constructor is called here
    }
}

    

Types of Constructors in Java

1. Default Constructor

If no constructor is defined in a class, Java automatically provides a default constructor with no parameters. You can also create your own no-argument constructor.


class Bike {
    Bike() {
        System.out.println("Default constructor called");
    }
}

    

2. Parameterized Constructor

A parameterized constructor allows you to pass values when creating objects. This is useful for setting initial property values.


class Student {
    String name;
    int age;

    Student(String n, int a) {
        name = n;
        age = a;
    }

    void display() {
        System.out.println(name + " - " + age);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Alice", 20);
        s1.display();
    }
}

    

Constructor Overloading

In Java, constructors can be overloaded by defining multiple constructors with different parameter lists. This provides flexibility in object creation.


class Rectangle {
    int length, width;

    Rectangle() {
        length = 0;
        width = 0;
    }

    Rectangle(int l, int w) {
        length = l;
        width = w;
    }

    void area() {
        System.out.println("Area: " + (length * width));
    }
}

    

Constructor Chaining

Constructor chaining allows one constructor to call another within the same class using the this() keyword. It helps avoid code duplication.


class Book {
    String title;
    int pages;

    Book() {
        this("Unknown", 0);
    }

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

    void display() {
        System.out.println(title + " - " + pages + " pages");
    }
}

    

Key Rules About Constructors

  • Constructors don’t have a return type, not even void.
  • If no constructor is written, Java provides a default one.
  • If a constructor is defined, Java no longer provides the default constructor.
  • this() must be the first statement if used in a constructor.

Difference Between Constructor and Method

Although constructors and methods may look similar, they serve different purposes. A constructor initializes an object, while a method defines behavior. Also, constructors don't have return types and are called automatically, whereas methods are called explicitly.

Conclusion

Understanding Constructors in Java is essential for building robust and flexible applications. Whether it’s initializing an object with default values or passing parameters during creation, constructors play a vital role in class design and object lifecycle management.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat