A class definition contains at least one constructor. The syntax for defining a constructor for a class is:

constructor <access-mode> <class-name> 
                ( [input <parameter> as <type-name>][,…]):
                <body of constructor>
                end constructor.  

Syntax Element

Description

access-mode

Specifies whether the constructor will be available to other parts of an application. A best practice is to define the constructor as PUBLIC so that other parts of the application can create instances of the class. For more information, see Access modes.

class-name

Must match the name of the class in the class definition

type-name

Can be one of the built-in or user-defined types.

parameter

The value that is used to set the property. It must match the type of the property.

You can use the New Constructor wizard to generate the code for a constructor (right-click, then select Source > New Constructor). If the constructor takes parameters, you must manually add them to the definition.

There are two common design patterns for constructors:
  1. Use a no-argument constructor and then use a PUBLIC method to initialize the data members of the instance.
  2. Use a constructor with parameters and use the parameters to initialize the data members of the instance in the body of the constructor.
Note: If you use the New Constructor wizard to generate the constructor, the first statement that is generated is the call to super(). This means the constructor for the super class will be called first. It is safe to keep this statement in your constructor, even if you have not defined a super class.
See also: