Java.Core.What is a “default constructor”?

What is a Default Constructor in Java?

A default constructor is a constructor with no parameters that initializes an object with default values. If a class does not define any constructor, Java automatically provides a default constructor.


1. Characteristics of a Default Constructor

Has the same name as the class.
No parameters (() empty parentheses).
Automatically created by Java if no constructor is defined.
Initializes instance variables with default values.
Called when an object is created without arguments.


2. Example: Default Constructor

class Animal {
    // Default constructor (no parameters)
    Animal() {
        System.out.println("An animal is created!");
    }
}

public class DefaultConstructorExample {
    public static void main(String[] args) {
        Animal a = new Animal(); // Calls the default constructor
    }
}

Output:

An animal is created!

🔹 The default constructor is called automatically when an object is created.


3. What If We Don’t Define a Constructor?

  • If a class has no constructor, Java automatically generates a default constructor.

Example: Implicit Default Constructor

class Car {
    // No constructor defined
}

public class ImplicitDefaultConstructor {
    public static void main(String[] args) {
        Car c = new Car(); // Java provides a default constructor
    }
}

Java generates:

Car() {
    super(); // Calls Object class constructor
}

✅ This means every object has a constructor, even if not explicitly written.

4. Default Values in a Default Constructor

When Java creates an object using the default constructor, instance variables are initialized with default values.

Example: Default Values

class Student {
    int id;
    String name;
    boolean isEnrolled;

    void display() {
        System.out.println("ID: " + id); // Default: 0
        System.out.println("Name: " + name); // Default: null
        System.out.println("Enrolled: " + isEnrolled); // Default: false
    }
}

public class DefaultValuesExample {
    public static void main(String[] args) {
        Student s = new Student(); // Uses default constructor
        s.display();
    }
}

Output (default values assigned by Java):

ID: 0
Name: null
Enrolled: false

🔹 Default values assigned to instance variables:

  • Numeric types (int, double, float)0
  • Booleanfalse
  • Objects (String, custom classes, etc.)null

5. What If a Class Has Other Constructors?

If a class defines at least one constructor, Java does not provide a default constructor.

🔹 Example: Class With a Constructor (No Default Constructor)

class Person {
    String name;

    // Parameterized constructor
    Person(String personName) {
        this.name = personName;
    }
}

public class NoDefaultConstructorExample {
    public static void main(String[] args) {
        Person p = new Person("Alice"); // Works
        // Person p2 = new Person(); // ❌ Compilation error (No default constructor)
    }
}

🚨 Error: If a class has a parameterized constructor, the compiler does not generate a default constructor.

Solution: Manually add a default constructor.

class Person {
    String name;

    // Default constructor
    Person() {
        this.name = "Unknown";
    }

    // Parameterized constructor
    Person(String name) {
        this.name = name;
    }
}

Now, both new Person(); and new Person("Alice"); will work!


6. Default Constructor vs. Parameterized Constructor

FeatureDefault ConstructorParameterized Constructor
Parameters?❌ No✅ Yes
Generated by Java?✅ Yes (if no constructor exists)❌ No
Initializes Fields?✅ With default values✅ With specified values
Can Be Defined Explicitly?✅ Yes✅ Yes

7. Summary

Key PointExplanation
What is a Default Constructor?A constructor without parameters that initializes an object with default values.
When is it Automatically Generated?If a class has no constructors, Java provides a default constructor.
What Does it Initialize?Instance variables with default values (0, null, false).
What If the Class Has Another Constructor?Java does not generate a default constructor if a parameterized constructor exists.

8. Conclusion

  • 🚀 A default constructor is automatically provided by Java if no constructor exists.
  • It initializes instance variables with default values.
  • ⚠️ If a class has any other constructor, Java does NOT provide a default constructor.
  • It can be explicitly defined if needed.
This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.