10 constructor types in Java
Constructors play a key role in all object-oriented programming languages, and Java is no exception.
Every class a Java developer creates needs a constructor. Constructors perform numerous important functions, including the following:
- Create instances of classes and records.
- Allocate memory for instances on the heap.
- Assign default values to variables.
- Traverse the class's inheritance hierarchy.
10 types of Java constructors
There is a variety of different types of constructors in Java, and each constructor type has its own special use case and is subject to its own specific set of rules. According to the Java language specification, there are 10 different constructor types in Java:
- Default constructor.
- Explicit constructors.
- Canonical constructors.
- Superclass constructors.
- Anonymous constructors.
- Inner class constructors.
- Enum constructors.
- Generic constructors.
- Private constructors.
- Overloaded constructors.
Subtypes of Java constructors
In common parlance, you may also hear developers speak about other types of Java constructors:
- Copy constructors.
- Parameterized constructors.
- No-args constructors.
- Non-default constructors.
However, technically all of these are subtypes of the 10 Java constructor types listed above.
Cameron McKenzie has been a Java EE software engineer for 20 years. His current specialties include Agile development, DevOps and container-based technologies, such as Docker, Swarm and Kubernetes.