2013 Jun 13 1:54 AM
Hello
I saw a standard SAP class in PPM module, it doesn't have GET_INSTANCE method, but it has CLASS_CONSTRUCTOR and CONSTRUCTOR methods, and also it has GET_ATTRIBUTES method (actually, i want to call this method in my_z_prog), well.
Now, i want to use GET_ATTRIBUTES method of this class in my_z_prog, but when i checked this class in SE24 i didn't find any GET_INSTANCE method, hence i thought i can't use this class bcz of no chance of instantiting of this class
But, one of another developer (now, rolled off and out of project) called the same mathod / class by using the below code
DATA: lr_std_sap_class TYPE REF TO std_sap_class
CREATE OBJECT lr_std_sap_class
EXPORTING
iv_employee_number = lv_my_number
CALL METHOD lr_std_sap_class->get_attributes
IMPORTING
es_attributes = ls_my_attributes
Pls. let me know
1) With out having GET_INSTANCE method in it how its possible to create a OBJECT from out side (my_z_prog)?
2) What is the difference between GET_INSTANCE method and CONSTRUCOR method?
3) In this case how we are getting instance by using CREATE OBJECT?
4) The above is safe? i mean, can go with the same approach?
When i tested this class in SE24 i saw the INSTANCE push button on the tool bar
Thank you
2013 Jun 13 4:45 AM
Hello
I saw a standard SAP class in PPM module, it doesn't have GET_INSTANCE method, but it has CLASS_CONSTRUCTOR and CONSTRUCTOR methods, and also it has GET_ATTRIBUTES method (actually, i want to call this method in my_z_prog), well.
Now, i want to use GET_ATTRIBUTES method of this class in my_z_prog, but when i checked this class in SE24 i didn't find any GET_INSTANCE method, hence i thought i can't use this class bcz of no chance of instantiting of this class
But, one of another developer (now, rolled off and out of project) called the same mathod / class by using the below code
DATA: lr_std_sap_class TYPE REF TO std_sap_class
CREATE OBJECT lr_std_sap_class
EXPORTING
iv_employee_number = lv_my_number
CALL METHOD lr_std_sap_class->get_attributes
IMPORTING
es_attributes = ls_my_attributes
Pls. let me know
1) With out having GET_INSTANCE method in it how its possible to create a OBJECT from out side (my_z_prog)?
2) What is the difference between GET_INSTANCE method and CONSTRUCOR method?
3) In this case how we are getting instance by using CREATE OBJECT?
4) The above is safe? i mean, can go with the same approach?
When i tested this class in SE24 i saw the INSTANCE push button on the tool bar
Thank you
2013 Jun 13 3:27 AM
CREATE OBJECT statement is used to create instance of a class, and using that object you can call instance methods, like get_attributes. During debug, you can see that when object is created, constructor method gets called.
On the other hand, get_instance is usually a static method, which creates object and return reference. When you check code inside get_instance, chances are that there would be CREATE OBJECT statement written inside it.
2013 Jun 13 3:53 AM
Hi,
CREATE OBJECT does the instantiation. Go through the ABAP OO learning series done by . Its the best way to start off and know the basics of OO ABAP.
http://scn.sap.com/docs/DOC-10236
Cheers,
Arindam
2013 Jun 13 4:45 AM
2013 Jun 13 6:02 AM
CREATE OBJECT is used to creates the instance of the class, which holds all the methods and attributes. It uses the Class's CONSTRUCTOR for creating the method. Whether you define the constructor or not, it will be present in the class whenever you create a class.
GET_INSTANCE is a user define method which returns the instance of the class. Usually this method is used only if the CONSTRUCTOR is private. This method comes into picture when we need SINGLETON CLASS (only one instance will be there through out the program).
The GET_INSTANCE method in turn uses the CREATE OBJECT to create an object if the object is not created previously.
Working:
Assume there is class with name ZCL_TEST.
1. There will be a global reference variable in the class of type same as the current class.
g_ref_instance TYPE REF TO zcl_test.
2. Make the constructor private, so that only through GET_INSTANCE method the objects can be created.
3. Create an method GET_INSTANCE which returns the reference of zcl_test.
METHOD get_instance.
IF g_ref_instance IS NOT BOUND.
CREATE OBJECT g_ref_instance TYPE zcl_test.
ENDIF.
rt_instance = g_ref_instance.
rt_instance is an RETURNING parameters of this method.
From the above you can see, if g_ref_instance in not created then it creates else returns the previous instance
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |