SCJP Certification

Sun Certified Java Programmer Certification exam essentials

September 29th, 2009

What is a constructor?

Uncategorized, by Daisy Williams.

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:

  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 is a constructor?”

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 *

*