{{short description|In programming, an object-creating function that takes no arguments}}

In computer programming, a '''nullary constructor''' is a constructor that takes no arguments.<ref>{{Cite web |date=2022-01-13 |title=Default Constructor in Java – Class Constructor Example |url=https://www.freecodecamp.org/news/default-constructor-in-java/ |access-date=2022-03-23 |website=freeCodeCamp.org |language=en}}</ref> Also known as a '''0-argument constructor''', '''no-argument constructor''',<ref>{{Cite web |title=No-argument Constructor |url=https://chortle.ccsu.edu/java5/Notes/chap50/ch50_13.html |access-date=2022-03-23 |website=chortle.ccsu.edu}}</ref> '''parameterless constructor''' or '''default constructor'''.<ref>{{Cite web |title=Default constructors - cppreference.com |url=https://en.cppreference.com/w/cpp/language/default_constructor |access-date=2023-04-12 |website=en.cppreference.com}}</ref>

==Object-oriented constructors== In object-oriented programming, a constructor is code that is run when an object is created. Default constructors of objects are usually nullary.<ref>{{Citation |last=Ottinger |first=Joseph B. |title=An Introduction to Hibernate 6 |date=2022 |url=https://doi.org/10.1007/978-1-4842-7337-1_1 |work=Beginning Hibernate 6: Java Persistence from Beginner to Pro |pages=1–25 |editor-last=Ottinger |editor-first=Joseph B. |place=Berkeley, CA |publisher=Apress |language=en |doi=10.1007/978-1-4842-7337-1_1 |isbn=978-1-4842-7337-1 |access-date=2022-03-23 |last2=Linwood |first2=Jeff |last3=Minter |first3=Dave |editor2-last=Linwood |editor2-first=Jeff |editor3-last=Minter |editor3-first=Dave|url-access=subscription }}</ref>

File:Nullary constructor UML.svg

===Java example=== <syntaxhighlight lang="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; } } </syntaxhighlight>

===C++ example=== <syntaxhighlight lang="cpp"> 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; } } </syntaxhighlight>

==Algebraic data types== 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.

===Haskell example=== <syntaxhighlight lang="haskell"> -- nullary type constructor with two nullary data constructors data Bool = False | True

-- non-nullary type constructor with one non-nullary data constructor data Point a = Point a a

-- non-nullary type constructor with... data Maybe a = Nothing -- ...nullary data constructor | Just a -- ...unary data constructor </syntaxhighlight>

== See also == * Default constructor

== References == {{Reflist}} {{DEFAULTSORT:Nullary Constructor}} Category:Method (computer programming) Category:Articles with example Haskell code Category:Articles with example Java code

{{Comp-sci-stub}}