Default Constructors in Java

java default constructors

Java uses constructors to create new objects from class blueprints. These routines are key for ensuring proper state upon object instantiation and offer developers lots of options. Whether you’re overloading, chaining, or overriding—constructors in Java can make programming much more efficient.

Before diving into Java’s implementation of constructors it’s important to note that Java does not require classes to define constructors explicitly. If a constructor isn’t explicitly defined, the compiler will automatically create a no-argument default constructor for the class.

Java Constructors

Constructors look a lot like methods. They have names, access modifiers, and can even accept arguments. The notable difference between a Java constructor and class method is that the constructor has the same name as the Class and no specified return type. Here are the main characteristics of the signature of a Java constructor:

  1. No return type
  2. Identical to Class name
  3. Optional parameters

When these characteristics are satisfied, Java uses a constructor method to instantiate a new class object with the desired state. When there is an error in these, Java will generate a compile-time exception. However, Java doesn’t require a constructor to be declared explicitly. In such cases, Java has a default constructor routine.

Java’s Default Constructor

When a constructor isn’t explicitly defined, Java will automatically generate a constructor based on an object’s superclass. In many cases, the superclass is the default Object class.

Per the Java Tutorial’s Documentation:

The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn’t have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.

This means, even if an object is expressing a complex series state of inheritance, the Java compiler will continue looking up the hierarchical tree until it finds a relative with a default no-argument constructor. Worst case scenario: it makes it all the way up to the Java Object model.

Default Constructor Example

In the example code below, there is no constructor explicitly defined for the Person object class.

public class Person{

    // Data members
    String name;
    int age;
}

This signals Java to use the default constructor from the Person class’s superclass which, in this case, is the Java Object class. Essentially, the code above is the same as the following:

public class Person{

    // Data members
    String name;
    int age;
    
    // Constructor
    Person(){
        super();
    }
}

This code is redundant and considered bad-practice. In this case, no functionality is being gained through explicitly calling the superclass’s default constructor. After all, it gets called if there is no explicit constructor. In the next example, the procedure for defining data member values and using the superclass constructor method is illustrated:

public class Person{

    // Data members
    String name;
    int age;

    // Constructor
    Person(){
        
        // Call parent constructor
        super();
        
        // Define data member values
        this.name = "John Doe";
        this.age = 35;
    }
}

Note: The call to the superclass must be made before setting values of the data members name and age or a compile-time exception will be thrown.

Explicit Constructor Example

Java constructors can be implemented in many ways depending on use-case. The following constructor in Java example has no frills and simply ensures the example Person object is instantiated with an application-supplied age and name field.

public class Person{
    
    // Data members
    String name;
    int age;
    
    // Constructor
    Person (String name, int age){
        this.name = name;
        this.age = age;
    } 
}

Default Access Modifiers

Java provides access to modifiers to enforce object security. These can be specified using reserved keywords such as private, public and protected. In the above examples, the Person class is always declared as being public. However, no access-control is explicitly declared for Person‘s constructor (when defined). In such cases, Java falls back to the default modifier which is package-private (R).

Review

Constructors are powerful design tools that help determine State upon class object instantiation. Java doesn’t require a class to explicitly define a constructor, but it will always ensure one is present by convention. In such cases where a user-defined class does not define a constructor explicitly, Java will look up the hierarchical tree of inheritance to the first relative with a default constructor—making it all the way to the Java Object class if necessary.

Zαck West
Full-Stack Software Engineer with 10+ years of experience. Expertise in developing distributed systems, implementing object-oriented models with a focus on semantic clarity, driving development with TDD, enhancing interfaces through thoughtful visual design, and developing deep learning agents.