Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

There is no single cross-language meaning of “default constructor.” In C++, it means any constructor callable with no arguments, whether written with an empty parameter list or with default values for all parameters. In Java and C#, the term usually means a no-argument constructor the compiler supplies only when the class declares no constructors. An explicitly written no-argument constructor is not the same as an automatically generated one.

What is the difference?

“No-argument” describes how a constructor can be called: the caller supplies no arguments. “Default constructor” is language-specific. It describes a constructor callable with no arguments in C++, but in Java and C# it generally names a constructor that the compiler provides under a particular condition.

That distinction matters when reading examples or deciding whether a class can be created without arguments. A class may have a no-argument constructor and still have no compiler-generated constructor.

C++: a default constructor is defined by how it can be called

In C++, a default constructor is any constructor that can be called with no arguments, as cppreference explains. It may have no parameters, or every parameter may have a default argument.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
struct A {
    A() {}                 // explicitly written; a default constructor
};

struct B {
    B(int value = 0) {}    // callable with no arguments; also a default constructor
};

struct C {
    C(int value) {}        // cannot be called with no arguments
};

For example, Microsoft’s constructor documentation shows a constructor whose width, length, and height parameters each have default values. That constructor can be called without supplying arguments, so it meets the C++ definition.

When C++ supplies one automatically

The compiler implicitly declares a default constructor only if the class has no user-declared constructor or constructor template. Declaring a constructor that requires arguments, such as C(int value), therefore prevents that implicit declaration; it does not give the class a way to be constructed with no arguments.

You can also explicitly request a defaulted constructor with = default. This is a programmer declaration, not implicit generation:

struct D {
    D() = default;
};

“Default” does not promise useful application-level values for every member. Initialization depends on the constructor and the members involved. Microsoft advises initializing members in the class definition when relying on an implicit default constructor.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Java: “default constructor” means the compiler-provided constructor

In Java, if a class contains no constructor declarations, the compiler implicitly declares a default constructor. The dev.java learning documentation describes it as a no-argument constructor supplied for a class without constructors.

class A {}                 // compiler declares a default constructor
class B { B() {} }         // explicit no-argument constructor
class C { C(int n) {} }    // no compiler-provided no-argument constructor

Writing B() gives the class a no-argument constructor, but it is explicit: because the class already declares a constructor, the compiler does not also supply a default one. Likewise, declaring only C(int n) means there is no no-argument constructor to call unless you declare one yourself.

Superclass constraint

Java’s generated constructor must also satisfy the language’s superclass-constructor rules. Under the Java Language Specification, Java SE 7 edition, compilation fails if the superclass does not have an accessible no-argument constructor that meets the specification’s conditions. The cited specification is SE 7; consult the edition matching your project for version-specific details.

C#: a similar rule for class constructors

In C#, a class receives a default instance constructor if it declares no instance constructors. An explicitly written parameterless constructor is still callable without arguments, but it is not the automatically provided constructor.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
class A {}                      // compiler provides a default instance constructor
class B { public B() {} }       // explicit parameterless constructor
class C { public C(int n) {} }  // no automatically provided class default constructor

The rule is described in the C# language specification. In the examples, declaring any instance constructor in the class suppresses the automatic class default constructor. If a no-argument construction path is needed, declare an appropriate parameterless constructor yourself.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Compare the terminology across languages

Language What “default constructor” means When an automatic constructor is supplied
C++ Any constructor callable with no arguments, including one whose parameters all have default arguments. When the class has no user-declared constructor or constructor template.
Java The no-argument constructor implicitly declared when a class has no constructor declarations. Only when the class declares no constructors; superclass accessibility rules can make compilation fail.
C# The default instance constructor automatically provided for a class with no instance-constructor declarations. Only when the class declares no instance constructors.

How to tell which constructor a class has

  1. Identify the language. Do not transfer the C++ definition of “default constructor” to Java or C#.
  2. Look at the declarations. Check whether the class explicitly declares a zero-parameter constructor, a constructor with defaulted parameters, or only constructors requiring arguments.
  3. Check the language’s generation rule. In Java and C#, any relevant constructor declaration prevents the compiler-provided default constructor. In C++, implicit declaration is prevented by a user-declared constructor or constructor template.
  4. Check constraints beyond the class. In Java, verify that the superclass has an accessible no-argument constructor meeting the applicable specification rules.
  5. Check initialization separately. A constructor’s name or default status does not establish that fields receive the values your program needs.

Common misconception: “default” does not mean “default values for every field”

The word refers to constructor definition or generation rules, not a universal guarantee of useful initialization. In C++, members’ initialization depends on how the constructor is defined and used. In Java and C#, a compiler-provided constructor does not mean the class has received application-specific setup. If code relies on particular values or invariants, define and verify that initialization explicitly.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.