Application Development 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: 

Reg: Contructor and methods

Former Member
0 Kudos

Hi all

i am new to abap objects and i am going thro' the sap materials and i am facing some probelm in understanding the constructor part which is also a type of method..can any one please let me know what is the difference between methods and constructors..

Your explanations are greatly welcome instead of links.

Thanks

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

actualy constructor is a special method in OOABAP which is used for initialisation and it is always named as constructor.

Constructors are of 2 type 1. CLASS or Static Constructor

and 2 . Method Constructor.

Check the following code for the use and effect of both types of constructors

REPORT demo_constructor_ANIRBAN.

----


  • 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( ).

11 REPLIES 11

Former Member
0 Kudos

Hi,

actualy constructor is a special method in OOABAP which is used for initialisation and it is always named as constructor.

Constructors are of 2 type 1. CLASS or Static Constructor

and 2 . Method Constructor.

Check the following code for the use and effect of both types of constructors

REPORT demo_constructor_ANIRBAN.

----


  • 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( ).

matt
Active Contributor
0 Kudos

>

....

Anirban - please learn to surround your code with It's much easier to read that way.

matt

0 Kudos

thanks for advice , Matthew.

i skipped it.

regards,

anirban

Former Member
0 Kudos

Hi,

Constructor is a special instance method, which is called at runtime when object is created. Each class can have only one instance constructor.

Methods are internal procedures in classes that determine the behavior of the objects. They can access all attributes in their class.

Regards

Abhijeet

Former Member
0 Kudos

This message was moderated.

sachin_soni
Active Participant
0 Kudos

This message was moderated.

Former Member
0 Kudos

Hi,

Constructors:

They are special kind of methods which are mainly used to initialise the values or the data in an object. Constructors are implicitly called as and when an object of the clas is created. We in Most of the cases does not call constructors.

Methods:

They are code block which are explictly called to perform some functionality.

Regards

Sumit Agarwal

Former Member
0 Kudos

Hello Arun,

Constructor is a special method, which will be called automatically once when you create a instance of that class.

For example you have your website and you wan to check no. of hits to our site. You will declare one method in that you will update some static variable. This method will automatically called when ever you create an instance in this case your instance is calling that web site.

I hope you understand the concept

Regards,

Naresh.

narin_nandivada3
Active Contributor
0 Kudos

Hi Arun,

Please check this thread

Hope this would help you.

Good luck

Narin

Former Member
0 Kudos

.

Edited by: Mahesh Reddy .Pocha on Sep 5, 2008 7:29 AM

Former Member
0 Kudos

,

Edited by: Mahesh Reddy .Pocha on Sep 10, 2008 8:37 AM