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 creation in custom clasess?

former_member202077
Participant
0 Likes
449

Hello,

Pls. let me know, What is the use of constructor, while creating the custom clasess (or) in the context of ABAP objects? actually, I have gone through the first 2 tutorials of this this link, [http://www.sdn.sap.com/irj/scn/weblogs;jsessionid=(J2EE3414800)ID1160600550DB10016261778120999663End?blog=/pub/wlg/15408] but, sorry to say that, i did not understand the content!! also, pls. let me know that, When we have to use "me"? wht are the pre-requisites for it?

Thank you

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
421

The CONSTRUCTOR of a class is a method which is called automatically when you create an instance of object of that class. So ABAP code, when you use the statement CREATE OBJECT <instance>. Then the CONSTRUCTOR method is called. This constructor method is used to "iniitialize" the instance(or object) but you can put ABAP code in there to do whatever you want really. This is not specific to ABAP, but to OO concepts. You should read a paper on Object Oriented concepts, and then try to apply them to ABAP, it will make more sense, I think.

As for as using "me". In ABAP, "Me" is basically a reference to the instance of an object. You use "me" when referring to the instance of the object in which you are working in. For example, lets say that you have an attribute in your class called foo. And you have a method in your class called, set_foo. In your SET_FOO method of the class, you would have access to this foo attribute. You could set it using the following syntax.

me->foo = 'This is foo'.

Likewise, lets say that you call the SET_FOO method in your CONSTRUCTOR method. You can use this syntax.

me->set_foo( ).

So again, "me" is simply referencing the instance of the class.

Regards,

Rich Heilman

Hello,

Pls. let me know, What is the use of constructor, while creating the custom clasess (or) in the context of ABAP objects? actually, I have gone through the first 2 tutorials of this this link, [http://www.sdn.sap.com/irj/scn/weblogs;jsessionid=(J2EE3414800)ID1160600550DB10016261778120999663End?blog=/pub/wlg/15408] but, sorry to say that, i did not understand the content!! also, pls. let me know that, When we have to use "me"? wht are the pre-requisites for it?

Thank you

1 REPLY 1
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
422

The CONSTRUCTOR of a class is a method which is called automatically when you create an instance of object of that class. So ABAP code, when you use the statement CREATE OBJECT <instance>. Then the CONSTRUCTOR method is called. This constructor method is used to "iniitialize" the instance(or object) but you can put ABAP code in there to do whatever you want really. This is not specific to ABAP, but to OO concepts. You should read a paper on Object Oriented concepts, and then try to apply them to ABAP, it will make more sense, I think.

As for as using "me". In ABAP, "Me" is basically a reference to the instance of an object. You use "me" when referring to the instance of the object in which you are working in. For example, lets say that you have an attribute in your class called foo. And you have a method in your class called, set_foo. In your SET_FOO method of the class, you would have access to this foo attribute. You could set it using the following syntax.

me->foo = 'This is foo'.

Likewise, lets say that you call the SET_FOO method in your CONSTRUCTOR method. You can use this syntax.

me->set_foo( ).

So again, "me" is simply referencing the instance of the class.

Regards,

Rich Heilman