A nullary constructor in computer programming is a constructor that takes no arguments. It is also commonly referred to as a 0-argument constructor, no-argument constructor, parameterless constructor, or default constructor. This type of constructor is responsible for initializing objects to their default state when no specific data is provided during creation.
Object-Oriented Constructors
In object-oriented programming (OOP), a constructor is a special method that runs automatically when an object is created. It prepares the new object for use by initializing variables or allocating resources. A nullary constructor is simply a version of this method that requires no parameters to be passed.
Most programming languages, such as Java, C++, and C#, allow programmers to define nullary constructors explicitly. If no constructors are defined, many languages automatically provide a default nullary constructor that initializes object members with default values (for example, zero, null, or false).
A nullary constructor ensures that a class can always be instantiated without arguments, supporting polymorphism and subclass inheritance.
Example in Java
public class MyInteger {
private int data;
// Nullary constructor
public MyInteger() {
this(0);
}
// Non-nullary constructor
public MyInteger(int value) {
this.data = value;
}
int getData() {
return data;
}
void setData(int value) {
data = value;
}
}
In this example, MyInteger() is a nullary constructor that sets data to zero. The second constructor accepts an integer argument and sets the object’s internal value accordingly.
Example in C++
class Integer {
private:
int data;
public:
// Default constructor with parameters
// Leaving parameters unspecified defaults to the default value
Integer(int value = 0) : data{value} {}
[[nodiscard]]
int getData() const noexcept {
return data;
}
void setData(int value) noexcept {
data = value;
}
};
Here, the default parameter value makes the constructor behave like a nullary constructor when no arguments are passed.
In Algebraic Data Types
In functional programming and type theory, the term “constructor” also refers to data constructors in algebraic data types (ADTs). A nullary data constructor is one that does not take any arguments and represents a constant value.
Example in Haskell:
-- Nullary type constructor with two nullary data constructors
data Bool = False | True
-- Non-nullary type constructor with one nullary data constructor
data Maybe a = Nothing | Just a
In this example, False, True, and Nothing are nullary data constructors, while Just a is unary, meaning it takes one argument.
Purpose and Use Cases
Nullary constructors serve several key purposes:
- Default initialization: Provide a clean, consistent way to initialize objects when no external data is available.
- Ease of subclassing: Allow derived classes to call parent constructors without requiring specific arguments.
- Simplified API design: Enable users to instantiate objects easily without parameters.
- Functional pattern consistency: In type systems like Haskell, allow consistent tagging of data variants with no associated payload.
Conclusion
The nullary constructor is fundamental to both object-oriented and functional programming paradigms. In OOP, it provides a standard mechanism for default object creation, while in functional programming, it defines constant data constructors. Its simplicity makes it a universal concept bridging programming styles, ensuring reliable initialization across systems and languages.






