Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Class Constructor
Types, Syntax & Examples



In Java, a constructor is a special method that's invoked when an object of a class is created. Unlike regular methods, constructors have the same name as the class and do not have a return type.

The main purpose of a constructor is to initialize objects.

Basic Syntax of a Constructor

class ClassName {
    ClassName() {
        // constructor body
    }
}

Types of Constructors in Java

Java provides three types of constructors:

1. Default Constructor

If you don't explicitly define any constructor, Java provides one by default. It initializes the object with default values.

class Student {
    Student() {
        System.out.println("Default Constructor called.");
    }
    
    void display() {
        System.out.println("Student details loaded.");
    }
    
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.display();
    }
}
Default Constructor called.
Student details loaded.

2. Parameterized Constructor

When you want to initialize an object with custom values, parameterized constructors come into play.

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

    void display() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
    
    public static void main(String[] args) {
        Student s1 = new Student("Alice", 20);
        Student s2 = new Student("Bob", 22);
        s1.display();
        s2.display();
    }
}
Name: Alice
Age: 20
Name: Bob
Age: 22

3. Copy Constructor

Java doesn’t provide a built-in copy constructor like C++, but you can create one manually to copy values from another object.

class Student {
    String name;
    int age;

    // Parameterized constructor
    Student(String n, int a) {
        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 static void main(String[] args) {
        Student s1 = new Student("Clara", 23);
        Student s2 = new Student(s1);  // copying s1 into s2
        s2.display();
    }
}
Name: Clara, Age: 23

Constructor Overloading

You can define multiple constructors in a class with different parameter lists. This is called constructor overloading.

class Rectangle {
    int length, breadth;

    // Constructor 1
    Rectangle() {
        length = 0;
        breadth = 0;
    }

    // Constructor 2
    Rectangle(int l, int b) {
        length = l;
        breadth = b;
    }

    void display() {
        System.out.println("Length: " + length + ", Breadth: " + breadth);
    }

    public static void main(String[] args) {
        Rectangle r1 = new Rectangle();
        Rectangle r2 = new Rectangle(5, 3);
        r1.display();
        r2.display();
    }
}
Length: 0, Breadth: 0
Length: 5, Breadth: 3

Key Points to Remember

Why Constructors Matter?

Constructors lay the foundation for your objects. They’re not just initializers—they help set the tone for object identity, behavior, and consistency right at the start. Think of them as the blueprint’s first breath into a living, breathing object in your program.

QUIZ

Question 1:Which of the following statements about Java constructors is true?

Question 2:If no constructor is explicitly defined in a Java class, the compiler automatically inserts a default constructor.

Question 3:What will be the output of the following code?

class Demo {
    Demo() {
        System.out.println("Default constructor");
    }
    public static void main(String[] args) {
        new Demo();
    }
}

Question 4:Which of the following are valid characteristics or behaviors of Java constructors?

Question 5:Copy constructors in Java are provided by default, similar to default constructors.

Question 6:In the code below, which constructor is being used for object creation?

Student s1 = new Student("Alice", 20);



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M