Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

RE:ABAP objects

alex_georgek
Associate
Associate
0 Likes
750

Hi,

What do u mean by a CONSTRUCTOR concept in OOPS ABAP....

Thanks in advance,

Alex.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
630

Hi,

Constructors are special PUBLIC methods that are triggered when an object is instantiated from a class. They 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.

To use the instance constructor, the CONSTRUCTOR method must be declared in the public section of the class using the METHODS statement, and implemented in the implementation section.

Constructors must always be declared in the PUBLIC section of a class.

Instance Constructor

Executed once for each instance.

Called automatically, immediately after the CREATE OBJECT statement.

Can contain an interface with IMPORTING parameters and EXCEPTIONS , but cannot have any EXPORTING/CHANGING/RETURNING parameters .

The interfaces are defined using the same syntax as for normal methods in the METHODS statement. To transfer parameters and handle exceptions, use the EXPORTING and EXCEPTIONS additions to the CREATE OBJECT statement .

Static Constuctor:

Static methods, declared as CLASS-METHODS : CLASS_CONSTRUCTOR in the public section of the class definition and are also implemented in the implementation part.

Has no interface parameters and cannot trigger exceptions.

Executed once in each program. It is called automatically for the class before it is accessed for the first time - that is, before one of the following actions:

CREATE OBJECT obj from the class.

Call a static method : [CALL METHOD] class=>meth.

Registering a static event handler method using SET HANDLER class=>meth for obj.

Registering an event handler method for a static event of the class class.

Addressing a static attribute with class=>a.

Exception:-

The static constructor is always called immediately before the action is executed, with one exception:

If the 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 the access occurs.

Caution:-

The static constructor must not access its own class. Otherwise a runtime error will occur.

The point at which the static constructor is executed has not yet been finalized, and it may yet be changed. SAP only guarantees that it will be executed before the class is accessed for the first time. It is recommended not to write programs that require the static constructor to be executed at the beginning of a processing block.

If it is helpful rewards points.

Regards

Pratap.M

3 REPLIES 3
Read only

Former Member
0 Likes
630

hi,

Special method for creating

objects with defined initial

state

Only has IMPORTING

parameters and

EXCEPTIONS

Exactly one constructor is

defined per class (explicitly

or implicitly)

Is executed exactly once per

instance

The constructor is a special (instance) method in a class and is always named CONSTRUCTOR. The

following rules apply:

Each class has exactly one constructor.

The constructor does not need to be defined if no implementation is defined.

The constructor is automatically called during runtime within the CREATE OBJECT statement.

If you need to implement the constructor, then you must define and implement it in the PUBLIC SECTION.

When EXCEPTIONS are triggered in the constructor, instances are not created (as of 4.6a), so no main

memory space is taken up.

You need to implement the constructor when, for example

You need to allocate (external) resources

You need to initialize attributes that cannot be covered by the VALUE supplement to the DATA statement

You need to modify static attributes

You cannot normally call the constructor explicitly.

The static constructor is a special static method in a class and is always named CLASS_CONSTRUCTOR.

It is executed precisely once per program. The static constructor of class <classname> is called

automatically before the class is first accessed, that is, before any of the following actions are executed:

Creating an instance in the class using CREATE OBJECT obj, where obj has the data type

REF TO <classname>.

Addressing a static attribute using <classname>=><an_attribute>.

Calling a static attribute using CALL METHOD <classname>=><a_classmethod>.

Registering a static event handler method using SET HANDLER <classname>=><handler_method> for

obj.

Registering an event handler method for a static event in class <classname>.

The static constructor cannot be called explicitly.

Hope this is helpful, Do reward.

Read only

Former Member
0 Likes
631

Hi,

Constructors are special PUBLIC methods that are triggered when an object is instantiated from a class. They 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.

To use the instance constructor, the CONSTRUCTOR method must be declared in the public section of the class using the METHODS statement, and implemented in the implementation section.

Constructors must always be declared in the PUBLIC section of a class.

Instance Constructor

Executed once for each instance.

Called automatically, immediately after the CREATE OBJECT statement.

Can contain an interface with IMPORTING parameters and EXCEPTIONS , but cannot have any EXPORTING/CHANGING/RETURNING parameters .

The interfaces are defined using the same syntax as for normal methods in the METHODS statement. To transfer parameters and handle exceptions, use the EXPORTING and EXCEPTIONS additions to the CREATE OBJECT statement .

Static Constuctor:

Static methods, declared as CLASS-METHODS : CLASS_CONSTRUCTOR in the public section of the class definition and are also implemented in the implementation part.

Has no interface parameters and cannot trigger exceptions.

Executed once in each program. It is called automatically for the class before it is accessed for the first time - that is, before one of the following actions:

CREATE OBJECT obj from the class.

Call a static method : [CALL METHOD] class=>meth.

Registering a static event handler method using SET HANDLER class=>meth for obj.

Registering an event handler method for a static event of the class class.

Addressing a static attribute with class=>a.

Exception:-

The static constructor is always called immediately before the action is executed, with one exception:

If the 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 the access occurs.

Caution:-

The static constructor must not access its own class. Otherwise a runtime error will occur.

The point at which the static constructor is executed has not yet been finalized, and it may yet be changed. SAP only guarantees that it will be executed before the class is accessed for the first time. It is recommended not to write programs that require the static constructor to be executed at the beginning of a processing block.

If it is helpful rewards points.

Regards

Pratap.M

Read only

matt
Active Contributor
0 Likes
630

Constructors are NOT always public (although they'll always appear to be). Sometimes it's useful to have the contractor private (this is controlled in SE24 through the properties tab of the class). The instantiation of classes is then handled by what are called "factory methods". Factory methods are static methods of a class that return a reference to a class instance. They're the ones that actually contain the instantiation code (CREATE OBJECT).

Some OO thought suggest that constructors should never be public - always private.

Matt