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

Constructor and Class constructor

Former Member
52,524

Hi all

Can any one explain me the functionality about Constructor and class constructor??

As well as normal methods, which you call using CALL METHOD, there are two special methods
called CONSTRUCTOR and CLASS_CONSTRUCTOR, which are automatically called when you
create an object (CONSTRUCTOR) or when you first access the components of a class
(CLASS_CONSTRUCTOR) 

I have read the above mentioned document from SAP Documents but i found it bit difficult to understand can any one please help me on this??

Thanks and Regards,

Arun Joseph

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
19,715

Hi,

Constructor

Implicitly, each class has an instance constructor method with the reserved name constructor and a static constructor method with the reserved name class_constructor.

The instance constructor is executed each time you create an object (instance) with the CREATE OBJECT statement, while the class constructor is executed exactly once before you first access a class.

The constructors are always present. However, to implement a constructor you must declare it explicitly with the METHODS or CLASS-METHODS statements. An instance constructor can have IMPORTING parameters and exceptions. You must pass all non-optional parameters when creating an object. Static constructors have no parameters.

Class Constructor

The static constructor is always called CLASS_CONSTRUCTER, and is called autmatically before the clas is first accessed, that is before any of the following actions are executed:

Creating an instance using CREATE_OBJECT

Adressing a static attribute using ->

Calling a ststic attribute using CALL METHOD

Registering a static event handler

Registering an evetm handler method for a static event

The static constructor cannot be called explicitly.

For better understanding check the following code:

REPORT z_constructor.

----


  • CLASS cl1 DEFINITION

----


*

----


CLASS cl1 DEFINITION.

PUBLIC SECTION.

METHODS:

add,

constructor IMPORTING v1 TYPE i

v2 TYPE i,

display.

CLASS-METHODS:

class_constructor.

PRIVATE SECTION.

DATA:

w_var1 TYPE i,

w_var2 TYPE i,

w_var3 TYPE i,

w_result TYPE i.

ENDCLASS. "cl1 DEFINITION

----


  • CLASS cl1 IMPLEMENTATION

----


*

----


CLASS cl1 IMPLEMENTATION.

METHOD constructor.

w_var1 = v1.

w_var2 = v2.

ENDMETHOD. "constructor

METHOD class_constructor.

WRITE:

/ 'Tihs is a class or static constructor.'.

ENDMETHOD. "class_constructor

METHOD add.

w_result = w_var1 + w_var2.

ENDMETHOD. "add

METHOD display.

WRITE:

/'The result is = ',w_result.

ENDMETHOD. "display

endclass.

" Main program----


data:

ref1 type ref to cl1.

parameters:

w_var1 type i,

w_var2 type i.

start-of-selection.

create object ref1 exporting v1 = w_var1

v2 = w_var2.

ref1->add( ).

ref1->d isplay( ).

Regards,

Anirban Bhattacharjee

12 REPLIES 12
Read only

Former Member
0 Likes
19,715

This message was moderated.

Read only

Former Member
0 Likes
19,715

Hiii!

Instance Constructor = The constructor is a special instance method in a class and is always named CONSTRUCTOR.

The constructor is automatically called at runtime with CREATE OBJECT statement.

Some Important points about constructor:

  • Each class can have only one constructor.

  • The constructor must be defined in PUBLIC SECTION.

  • The constructor's signature can have only importing parameters and exceptions.

  • When exceptions are raised in the constructor, instances are not created so no main memory is occupied.

Static Constructor = This is a special static method in a class and is always names CLASS_CONSTRUCTOR. It is executed once per program. This constructor is called automatically before the class is first accessed, but before any of the following actions are executed for the first time:

  • Creating instance of this class(CREATE OBJECT)

  • Accessing a static attribute of this class.

  • Calling a static method of this class.

  • Registering an event handler method for an event in this class.

Some important points:

  • Each class has only one static constructor

  • This constructor must be defined in PUBLIC SECTION.

  • The constructor's signature cannot have importing parameters or exceptions.

  • The static constructor cannot be called explicitly

Regards

Abhijeet Kulshreshtha

Edited by: Abhijeet Kulshreshtha on Jul 9, 2008 6:16 AM

Read only

Former Member
0 Likes
19,715

Hey!

Thanks Joseph

Read only

19,715

Hi

Can any one let me know what is the difference between constructor and methods??

Thanks and Regards,

Arun Joseph

Read only

Former Member
0 Likes
19,716

Hi,

Constructor

Implicitly, each class has an instance constructor method with the reserved name constructor and a static constructor method with the reserved name class_constructor.

The instance constructor is executed each time you create an object (instance) with the CREATE OBJECT statement, while the class constructor is executed exactly once before you first access a class.

The constructors are always present. However, to implement a constructor you must declare it explicitly with the METHODS or CLASS-METHODS statements. An instance constructor can have IMPORTING parameters and exceptions. You must pass all non-optional parameters when creating an object. Static constructors have no parameters.

Class Constructor

The static constructor is always called CLASS_CONSTRUCTER, and is called autmatically before the clas is first accessed, that is before any of the following actions are executed:

Creating an instance using CREATE_OBJECT

Adressing a static attribute using ->

Calling a ststic attribute using CALL METHOD

Registering a static event handler

Registering an evetm handler method for a static event

The static constructor cannot be called explicitly.

For better understanding check the following code:

REPORT z_constructor.

----


  • CLASS cl1 DEFINITION

----


*

----


CLASS cl1 DEFINITION.

PUBLIC SECTION.

METHODS:

add,

constructor IMPORTING v1 TYPE i

v2 TYPE i,

display.

CLASS-METHODS:

class_constructor.

PRIVATE SECTION.

DATA:

w_var1 TYPE i,

w_var2 TYPE i,

w_var3 TYPE i,

w_result TYPE i.

ENDCLASS. "cl1 DEFINITION

----


  • CLASS cl1 IMPLEMENTATION

----


*

----


CLASS cl1 IMPLEMENTATION.

METHOD constructor.

w_var1 = v1.

w_var2 = v2.

ENDMETHOD. "constructor

METHOD class_constructor.

WRITE:

/ 'Tihs is a class or static constructor.'.

ENDMETHOD. "class_constructor

METHOD add.

w_result = w_var1 + w_var2.

ENDMETHOD. "add

METHOD display.

WRITE:

/'The result is = ',w_result.

ENDMETHOD. "display

endclass.

" Main program----


data:

ref1 type ref to cl1.

parameters:

w_var1 type i,

w_var2 type i.

start-of-selection.

create object ref1 exporting v1 = w_var1

v2 = w_var2.

ref1->add( ).

ref1->d isplay( ).

Regards,

Anirban Bhattacharjee

Read only

Former Member
0 Likes
19,715

hi,

A constructor is a member function of a class that is used to create objects of that class. It is alway named as constructor, has no return type, and is invoked using the new operator.

A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.

Regards,

Anirban Bhattacharjee

Read only

0 Likes
19,715

Hi

Can u please bit more Elaborative about the differences between methods and constructors??

Read only

0 Likes
19,715

Hiii!

Methods are internal procedures in classes that determine the behaviour of the objects. they can access all attributes in their class and can therefore change the state of other elements.

Methods have a signature that enables them to receive values WHEN THEY ARE CALLED and pass values back to the calling program.Methods can have any number of importing, exporting and changing parameters.

Constructors are basically a method which are called automatically at runtime with the CREATE OBJECT statement.

CONSTRUCTORS CAN ONLY BE DECLARED IN PUBLIC SECTION, WHILE METHODS CAN BE DECLARED BOTH IN PRIVATE AND PUBLIC SECTION.

Regards

Abhijeet

Read only

0 Likes
19,715

Hi

In what case we are using constructors??

Thanks

Arun Joseph

Read only

19,715

Hey Joseph!

Just check out this link, all your doubts regarding ABAP Objects will be cleared

http://www.sap-press.de/katalog/buecher/htmlleseproben/gp/htmlprobID-28?GalileoSession=22448306A3l7U...

Regards

Abhijeet Kulshreshtha

Read only

former_member778361
Discoverer
0 Likes
19,715

Their are two types of constructor : 1. Instance Constructor 2.Static constructor

instance constructor are the default constructor which will trigger automatically when we try to create the object of particular class.

for instance constructor we use :

Methods : constructor.

Static constructor are the special kind of method which will trigger automatically when the class will load onto the memory.

for static constructor we use :

class-Methods : class_constructor.

Read only

0 Likes
19,715

Thanks for coming to SAP Community for answers. Please post your question as a new question here:

Since you're new in asking questions here, check out our tutorial about asking and answering questions (if you haven't already), as it provides tips for preparing questions more effectively, that draw responses from our members.

Please note, that your post here won't be answered.