Tuesday, June 23, 2009

Constructors In Components of Classes

Constructors are special methods that produce a defined initial state for objects and classes. The state of an object is determined by its instance attributes and static attributes. You can assign contents to attributes using the VALUE addition in the DATA statement. Constructors are necessary when you want to set the initial state of an object dynamically.

Like normal methods, there are two types of constructor - instance constructors and static constructors.

For inheritance, some special rules apply to constructors that are not described here. These are covered in more detail in the inheritance context.

Instance Constructors

Each class has one instance constructor. It is a predefined instance method of the CONSTRUCTOR class. If you want to use the instance constructor, the constructor method must be declared in a visibility area of the class using the METHODS statement, and implemented in the implementation section. The visibility area in which the instance constructor can be declared, must be more general or equal to the instantiability defined by the addition CREATE of the CLASS statement, where the most specialized area is recommended. Unless it is explicitly declared, the instance constructor is an implicit method, which inherits and accesses the interface from the instance constructor in the upper class.

The declaration is carried out in a visibility area of the declaration part due to technical reasons. The actual visibility of the instance constructor is controlled by the CREATE PUBLIC|PROTECTED|PRIVATE additions of the CLASS statement. For more information refer to Visibility of Instance Constructors.

Instance constructors are called once for each instance. They are called automatically, immediately after you have created an instance using the CREATE OBJECT statement. It is not possible to call an instance constructor directly using the CALL METHOD statement.

An instance constructor can contain an interface with IMPORTING parameters and exceptions. You define the interface using the same syntax as for normal methods in the METHODS statement. The fact that there are no exporting parameters shows that constructors serve only to define the state of an object and have no other function. To transfer parameters and handle exceptions, use the EXPORTING and EXCEPTIONS additions to the CREATE OBJECT statement.

Static Constructors

Each class has a single static constructor. This is a predefined, public, static method of the class named constructor. If you want to use the static constructor, you must declare the static method class_constructor in the public section of the declaration part of the class using the CLASS-METHODS statement, and implement it in the implementation part. The static constructor has no interface parameters and cannot trigger exceptions. Unless you declare it explicitly, it is merely an empty method.

The static constructor is called once per class and internal session. It is called automatically for the class class before the class is accessed for the first time - that is, before one of the following actions:

  • Generating an instance of a class using CREATE OBJECT obj, where obj has the data type REF TO class.
  • Calling a static method using [CALL METHOD] class=>meth.
  • Registering a static event handler using SET HANDLER class=>meth for obj.
  • Registering an event handler for a static event of the class class.
  • Addressing a static attribute with class=>a.

The static constructor is always called immediately before the action is executed, with one exception: If your first access to the class is to address a static attribute, the static constructor is executed at the beginning of the processing block (dialog module, event block, procedure) in which access takes place.

Notes

  • The point at which the static constructor is called has not yet been finalized. SAP only guarantees that it will be called before the class is accessed for the first time. For this reason, static methods may be executed before the static constructor was ended.
  • The execution sequence of static constructors is dependent on the program flow. Static constructors must be implemented in such a way that they can be executed in any sequence.

No comments:

Blog Archive