In computer programming, a nullary constructor is a constructor that takes no arguments. [1] Also known as a 0-argument constructor, no-argument constructor, [2] parameterless constructor or default constructor. [3]
In object-oriented programming, a constructor is code that is run when an object is created. Default constructors of objects are usually nullary. [4]
publicclassMyInteger{privateintdata;// Nullary constructorpublicMyInteger(){this(0);}// Non-nullary constructorpublicMyInteger(intvalue){this.data=value;}intgetData(){returndata;}voidsetData(intvalue){data=value;}}
classInteger{private:intdata;public:// Default constructor with parameters// Leaving parameters unspecified defaults to the default valueInteger(intvalue=0):data{value}{}[[nodiscard]]intgetData()constnoexcept{returndata;}voidsetData(intvalue)noexcept{data=value;}}
In algebraic data types, a constructor is one of many tags that wrap data. If a constructor does not take any data arguments, it is nullary.
-- nullary type constructor with two nullary data constructorsdataBool=False|True-- non-nullary type constructor with one non-nullary data constructordataPointa=Pointaa-- non-nullary type constructor with...dataMaybea=Nothing-- ...nullary data constructor|Justa-- ...unary data constructor