Constructors implement the actions required to initialize instances of a class. Constructors are declared using constructor-declarations:
A constructor-declaration may include set of attributes (§17) and a valid combination of the four access modifiers (§10.2.3).
The identifier of a constructor-declarator must name the class in which the constructor is declared. If any other name is specified, an error occurs.
The optional formal-parameter-list of a constructor is subject to the same rules as the formal-parameter-list of a method (§10.5). The formal parameter list defines the signature (§3.4) of a constructor and governs the process whereby overload resolution (§7.4.2) selects a particular constructor in an invocation.
Each of the types referenced in the formal-parameter-list of a constructor must be at least as accessible as the constructor itself (§3.3.4).
The optional constructor-initializer specifies another constructor to invoke before executing the statements given in the block of this constructor. This is described further in §10.10.1.
The block of a constructor declaration specifies the statements to execute in order to initialize a new instance of the class. This corresponds exactly to the block of an instance method with a void
return type (§10.5.7).
Constructors are not inherited. Thus, a class has no other constructors than those that are actually declared in the class. If a class contains no constructor declarations, a default constructor is automatically provided (§10.10.4).
Constructors are invoked by object-creation-expressions (§7.5.10.1) and through constructor-initializers.