DEV Community

Ajay Sundar
Ajay Sundar

Posted on

Default constructor in java

What is a default constructor?

A default constructor (often called a no-argument constructor) is a constructor that takes no parameters or if it has parameters,all the parameters have default values.If there is no constructor in a class the compiler automatically creates an default constructor

An Example on Default Constructor

class Animals {
    String breed;
    int age;
    double weight;

    Animals(String breed, int age, double weight) {
        this.breed = breed;
        this.age = age;
        this.weight = weight;
    }

    Animals(String breed, int age) {
        this.breed = breed;
        this.age = age;
    }

    Animals() {
    }

    void Getdata() {
        System.out.println("breed:" + breed);
        System.out.println("age:" + age);
        System.out.println("weight:" + weight);
    }

    public static void main(String[] args) {
        Animals obj = new Animals("Goldenretreiver", 13, 43.0);
        obj.Getdata();
    }
}

Enter fullscreen mode Exit fullscreen mode
OUTPUT

breed:Goldenretreiver
age:13
weight:43.0

Enter fullscreen mode Exit fullscreen mode

1. class Animals { ... }

Declares a class named Animals. A class is a blueprint for objects (instances).

2. String breed; int age; double weight;

These are instance fields (properties) of the class. When an Animals object is created, each instance gets its own breed, age, and weight.

Default values if not explicitly set: breed → null, age → 0, weight → 0.0.

3. Animals(String breed, int age, double weight) { ... }

This is a parameterized constructor that receives three arguments and assigns them to the fields.

4. this.breed = breed;

uses the this keyword to refer to the instance field breed. This disambiguates the field from the parameter with the same name.

5. Animals(String breed, int age) { ... }

This is an overloaded constructor that accepts only breed and age. It sets these fields and leaves weight with its default value 0.0.

6. Animals() { }

This is an explicit no-argument constructor (explicit default). Because you have other constructors, this explicit no-arg constructor is required if you want to be able to do new Animals() elsewhere. It does nothing, so fields keep their default values.

7. void Getdata() { ... }

A method that prints the current values of the fields. Minor issues to note:

  • Naming convention: Java methods typically use camelCase and start with a lowercase letter (e.g., getData() or printData() is preferred).

  • Formatting: System.out.println("weight" + weight); produces weight43.0 (no colon or space). Better would be "weight: " + weight.

8.public static void main(String[] args) { ... }

Program entry point. This line creates a new Animals object using the three-argument constructor:

new Animals("Goldenretreiver", 13, 43.0); — calls the 3-parameter constructor, so breed, age, and weight are all set.

Then obj.Getdata(); prints those values.

Expected Output

breed:Goldenretreiver
age:13
weight:43.0

Enter fullscreen mode Exit fullscreen mode

Top comments (0)