What are the conventions for writing constructors?
Posted by Daisy WilliamsDec 2
A constructor provides a convenient way to initialize a newly created object at the time of its creation. It is defined for a class and it is called automatically immediately after the creation of a new object, but before the new operator completes. All 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.
No comments