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:

  1. A constructor is always given the name of the class in which it is defined.
  2. 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.
  3. 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.
  • Share/Bookmark