Welcome to SAP Central
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Static and instance constructor in ABAP Objects

Go down

Static and instance constructor in ABAP Objects Empty Static and instance constructor in ABAP Objects

Post  Uma_ABAP Sat Mar 31, 2012 11:43 pm


As we know c onstructors are special methods that cannot be called using CALL METHOD.
Instead, they are called automatically by the system to set the starting state of a new
object or class. There are two types of constructors - instance constructors and static
constructors. Constructors are methods with a predefined name. To use them, you must
declare them explicitly in the class.

The instance constructor of a class is the predefined instance method CONSTRUCTOR. You
declare it in the public section as follows:

METHODS CONSTRUCTOR
IMPORTING.. [VALUE(]<ii>[)] TYPE type [OPTIONAL]..
EXCEPTIONS.. <ei>.

and implement it in the implementation section like any other method. The system calls the
instance constructor once for each instance of the class, directly after the object has been
created in the CREATE OBJECT statement. You can pass the input parameters of the
instance constructor and handle its exceptions using the EXPORTING and EXCEPTIONS
additions in the CREATE OBJECT statement.

The static constructor of a class is the predefined static method CLASS_CONSTRUCTOR.
You declare it in the public section as follows:

CLASS-METHODS CLASS_CONSTRUCTOR.

and implement it in the implementation section like any other method. The static constructor
has no parameters. The system calls the static constructor once for each class, before the
class is accessed for the first time. The static constructor cannot therefore access the
components of its own class.

Uma_ABAP

Posts : 56
Join date : 2012-03-31

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum