SCJP Certification

Sun Certified Java Programmer Certification exam essentials

December 2nd, 2009

What are the conventions for writing constructors?

Java Facts, by Daisy Williams.

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

Back Top

Responses to “What are the conventions for writing constructors?”

Comments (0) Trackbacks (0) Leave a comment Trackback url
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Your email address will not be published. Required fields are marked *

*