# Nullary constructor

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Nullary_constructor
> Markdown URL: https://mediated.wiki/source/Nullary_constructor.md
> Source: https://en.wikipedia.org/wiki/Nullary_constructor
> Source revision: 1309949966
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

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

In [computer programming](/source/computer_programming), a '''nullary constructor''' is a [constructor](/source/Constructor_(computer_science)) that takes no [argument](/source/Parameter_(computer_science))s.<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](/source/object-oriented_programming), a [constructor](/source/constructor_(computer_science)) is code that is run when an [object](/source/Object_(computer_science)) is created. [Default constructor](/source/Default_constructor)s 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](/source/nodiscard)
    int getData() const noexcept {
        return data;
    }

    void setData(int value) noexcept {
        data = value;
    }
}
</syntaxhighlight>

==Algebraic data types==
In [algebraic data types](/source/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](/source/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}}

---
Adapted from the Wikipedia article [Nullary constructor](https://en.wikipedia.org/wiki/Nullary_constructor) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Nullary_constructor?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
