Constructors are typically used to create an object that is an instance of a class. A constructor is written by using the following general syntax:
[access modifier] class name([formal parameter list]) [throws clause] {
// Body of the constructor
}
where, the elements within square brackets are optional.
Although constructors look the same as methods, they are written by using the following conventions:
- A constructor is always given the name of the class in which it is defined.
- A constructor is always written without an explicit return type, not even void. This is because the implicit return type of a class constructor is the class itself.
- Only accessibility modifiers can be used to declared a constructor. Unlike methods, a constructor cannot be declared as abstract, static, final, native, strictfp, or synchronized.
Responses to “What is a constructor?”