Constructors

Purpose:

To initialize class objects.

Mechanics:

<modifiers> <class name>(<parameter list>) {
/* statements to initialize the object */
}

Example:

public class myClass {
/* Instance variables declared */
public myClass() { // the Constructor
/* statements to initialize myClass */
}
}

Usage:

  • A constuctor is a method with the same name as the class.
  • The constructor is invoked automatically each time an object of that class is instantiated.
  • Constructors may be overloaded to provide a variety of means for initializing an object.

Restrictions:

Constructors cannot specify return types or return values.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu