What is "constructed" in Java?

In Java, when you create an object from a class, it is called constructing an object.

For example:

Car myCar = new Car();

Here, new Car() is constructing an object of the class Car.

What is a Constructor?$

$ A constructor is a special method in a class.

$ It is used to initialize objects.

$ It has the same name as the class.

$ It has no return type, not even void.

Constructor Overloading in Java

Constructor Overloading means having more than one constructor in the same class with different parameter lists.

Key Points:

1.Only methods have return types.

Example: int add(), void print()

2.Constructors do not have return types (not even void).

$ They automatically return the class object.

3.Constructors are used to initialize objects.

4.Overloaded constructors help initialize objects in different ways.

Example:Constructor Overloading

class Student {
    String name;
    int age;

    // Constructor 1
    Student() {
        name = "Unknown";
        age = 0;
    }

    // Constructor 2
    Student(String n) {
        name = n;
        age = 0;
    }

    // Constructor 3
    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();
        Student s2 = new Student("Sugumar");
        Student s3 = new Student("Sugumar", 21);

        s1.display(); // Unknown - 0
        s2.display(); // Sugumar - 0
        s3.display(); // Sugumar - 21
    }
}

Quick Summary Poids (Points):

$ Constructor is used to initialize objects.

$ Constructor does not have a return type.

$ *Method has a return type" (like void, int, String).

$ Constructor name = Class name.

$ Constructor Overloading = *Multiple constructors with different parameters."

🧱 1. Default Constructor

A default constructor is a no-argument constructor that the Java compiler automatically provides if no other constructors are defined in the class. It initializes object fields with default values.

Example:

class Person {
    String name;
    int age;

    // Default constructor
    Person() {
        name = "Unknown";
        age = 0;
    }
}

In this example, when a Person object is created without any arguments, the default constructor initializes name to "Unknown" and age to 0.

📋 2. Copy Constructor

A copy constructor creates a new object by copying the fields of an existing object. It's particularly useful when you want to duplicate an object with the same state.

Example:

class Person {
    String name;
    int age;

    // Copy constructor
    Person(Person p) {
        this.name = p.name;
        this.age = p.age;
    }
}

Here, this.name = p.name; and this.age = p.age; copy the values from the existing Person object p to the new object.

🔑 3. Using this Keyword$$

When constructor parameters have the same names as class fields, the this keyword distinguishes between them. It refers to the current object's fields.

Example:

class Person {
    String name;
    int age;

    // Parameterized constructor
    Person(String name, int age) {
        this.name = name; // 'this.name' refers to the class field, 'name' refers to the parameter
        this.age = age;   // Similarly, 'this.age' is the class field, 'age' is the parameter
    }
}

Without the this keyword, assignments like name = name; would refer to the parameter itself, not the class field, leading to no change in the object's state.

📝 Summary Points

1. Default Constructor:Automatically provided by Java if no other constructors are defined; initializes fields with default values.

2. Copy Constructor: Manually defined to create a new object by copying another object's fields.

3. this Keyword: Used to refer to the current object's fields, especially when parameter names shadow field names.