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

Reg : constructors

Former Member
0 Likes
1,151

Hi ,

Could we apply the same concept of Constructors in JAVA for ABAP.

I learned the point that Constructors help in creating the object but doesnot create the object.

But without constructor its not possible to create objects .

Is the above point same in ABAP.?

System will introduce constructors at the time of object creation. How can I see the constructor induced?

regards

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
1,105

Hello

If an ABAP class has an explicit CONSTRUCTOR method it is called when the CREATE OBJECT <abap class> statement occurs.

Without an explicit CONSTRUCTOR method the implicit CONSTRUCTOR of the root class OBJECT is called.

Thus, the concepts of creating object instances is the same in ABAP and Java.

Regards

Uwe

8 REPLIES 8
Read only

Former Member
0 Likes
1,105

Hii!

It is true that constructor helps in creating objects. But its false that without constructor you can't create object

Read this

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.

Regards

Abhijeet

Read only

uwe_schieferstein
Active Contributor
0 Likes
1,106

Hello

If an ABAP class has an explicit CONSTRUCTOR method it is called when the CREATE OBJECT <abap class> statement occurs.

Without an explicit CONSTRUCTOR method the implicit CONSTRUCTOR of the root class OBJECT is called.

Thus, the concepts of creating object instances is the same in ABAP and Java.

Regards

Uwe

Read only

0 Likes
1,105

hi

So constructors help in creating objects and without it we cannot create objects. am I right Uwe Schieferstein

and what about my second que?

System will introduce implicit constructors at the time of object creation. How can I see the constructor induced?

plz help me yar

Read only

0 Likes
1,105

Hello

I think you cannot "see" (or debug) the implicit CONSTRUCTOR method.

Why?

If you have a table type (e.g. SWF_UTL_OBJECT_TAB) in your SAP system which has as structure REFERENCE TYPE to OBJECT (= root object of all ABAP classes) you will see why.

When you double-click on OBJECT you will not see the class OBJECT (in SE24) but receive the message "OBJECT is a type that is predefined in ABAP".

Anyway it would not be very exciting to debug the implicit instantiation because nothing happens except for the instantiation of your class (no presetting of attributes etc. because OBJECT as super-class has no attributes).

Regards

Uwe

Read only

0 Likes
1,105

hi,

So constructors help in creating objects and without it we cannot create objects. am I right Uwe Schieferstein

please answer my que,

and one more help could you provide me a scenario/example where explicit constructor is mandatory to use

regards

Read only

0 Likes
1,105

Hello

Technically the CONSTRUCTOR method is optional.

However, if you want to set certain attributes of your instance once and for all then the CONSTRUCTOR is semantically mandatory because it is the very first method to be called and it is called once and only once.

Example: You have a class dealing with purchase orders. In this case your class should have a CONSTRUCTOR method with an IMPORTING parameter ID_PO_NUMBER. The instance attribute <class name>->MD_PO_NUMBER may be public but must be read-only because otherwise you could change the identity of the instance during runtime.

Regards

Uwe

Read only

Former Member
0 Likes
1,105

hi,

Plz refer to the link and read the Constructor section.

http://searchsap.techtarget.com/tip/0,289483,sid21_gci1270269,00.html

Regards

Sumit Agarwal

Read only

Former Member
0 Likes
1,105

Hi,

You can very well relate the constructor concept in JAVA with ABAP Objects, with the exception that in JAVA, there is no static constructor; atleast not open to developers.

I hope everyone agrees with me. Is there any place in JAVA where you can write logic when a class is instantiated for the first time? Maybe you can write it in a normal constructor with some condition so that it gets executed only for the first instance.

Otherwise, the instance constructor in ABAP Objects works the same way as the constructor in JAVA.

Experts, please correct me if I'm wrong.