J
Java Basics Course
🔍
Question 9 • Page 4

What is a Constructor in Java?

  • Purpose: Constructor ek special member hai jiska upyog object ko initialize karne ke liye kiya jata hai. A special member used to initialize objects when they are created.
  • Rules: Constructor ka naam class ke naam ke saman hota hai aur iska koi return type nahi hota — void bhi nahi! Must have the exact same name as the class and has no return type (not even void).
  • When Called: Jab hum new keyword se object banate hain, to constructor automatically call hota hai. Automatically invoked whenever an object is created with the new keyword.

Types of Constructors in Java

1. Default Constructor

Compiler khud provide karta hai jab koi constructor na likha ho.

Provided by Java compiler if no constructor is defined.

Default
class S {
    int age;
    public static void main(String[] args) {
        S s1 = new S();
        System.out.println(s1.age);
    }
}
Output0

2. No-Argument Constructor

Programmer bina parameters ke khud define karta hai.

Explicitly defined by the programmer with zero parameters.

No-Arg
class S {
    S() {
        System.out.println("No-argument");
    }
    public static void main(String[] args) {
        S s1 = new S();
    }
}
OutputNo-argument

3. Parameterized Constructor

Parameters lekar object ko custom values se initialize karta hai.

Accepts parameters to initialize an object with specific values.

Parameterized
class S {
    int age;
    S(int age) {
        this.age = age;
    }
    void d() {
        System.out.println(age);
    }
    public static void main(String[] args) {
        S s1 = new S(20);
        s1.d();
    }
}
Output20
Page 4 (Cont.)

Constructor Overloading

Ek hi class mein alag-alag parameters ke saath ek se adhik constructors define karne ko Constructor Overloading kehte hain.

Defining multiple constructors in the same class with different parameter lists is called Constructor Overloading.

StudentOverloading.java
class Student {
    int age;
    String name;

    // Constructor 1: No-argument
    Student() {
        age = 18;
        name = "Unknown";
    }

    // Constructor 2: One parameter
    Student(int a) {
        age = a;
        name = "Unknown";
    }

    // Constructor 3: Two parameters
    Student(int a, String n) {
        age = a;
        name = n;
    }

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

    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student(20);
        Student s3 = new Student(22, "Gautam");

        s1.display();
        s2.display();
        s3.display();
    }
}
Expected OutputUnknown 18 Unknown 20 Gautam 22
Page 4 (Cont.)

Copy Constructor in Java

Note: Java mein Copy Constructor bhi banaya ja sakta hai, lekin Java mein yeh C++ ki tarah built-in constructor type nahi hai.
Note: You can create a Copy Constructor manually in Java, though unlike C++, Java does not provide an automatic built-in copy constructor.
CopyConstructorDemo.java
class Student {
    String name;
    int age;

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

    // User-defined Copy Constructor
    Student(Student s) {
        name = s.name;
        age = s.age;
    }

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

    public static void main(String[] args) {
        Student s1 = new Student("John", 20);
        Student s2 = new Student(s1);

        s1.display();
        s2.display();
    }
}
Expected OutputJohn 20 John 20