‎2007 Jun 25 9:40 AM
Hello
Why we go with constructor and give some sample OOprograming with class container.
Points for sure
‎2007 Jun 25 9:42 AM
<b>Constructors</b>
Constructors are special methods that produce a defined initial state of objects and classes. The state of an object is determined by its instance attributes and static attributes. You can assign contents to attributes using the VALUE addition in the DATA statement. Constructors are necessary when you want to set the initial state of an object dynamically.
Like normal methods, there are two types of constructor - instance constructors and static constructors.
For inheritance some special rules apply to constructors that are not described here but in the inheritance context.
Instance Constructors
Each class has one instance constructor. It is a predefined instance method of the CONSTRUCTOR class. If you want to use the instance constructor, the CONSTRUCTOR method must be declared in the public section of the class using the METHODS statement, and implemented in the implementation section. Unless it is explicitly declared, the instance constructor is an empty method.
The declaration in the public area is necessary for technical reasons. The actual visibility of the instance constructor is controlled by the CREATEPUBLIC|PROTECTED|PRIVATE additions to the CLASS statement. For more information refer to Visibility of Instance Constructors.
Instance constructors are executed once for each instance. They are called automatically, immediatly after you have created an instance using the CREATE OBJECT statement. It is not possible to call an instance constructor directly using the CREATE OBJECT statement.
An instance constructor can contain an interface with IMPORTING parameters and EXCEPTIONS. You define the interface using the same syntax as for normal methods in the METHODS statement. The fact that there are no exporting parameters shows that constructors only exist to define the state of an object and have no other function. To transfer parameters and handle exceptions, use the EXPORTING and EXCEPTIONS additions to the CREATE OBJECT statement (see the example in the documentation for CREATE OBJECT).
Static Constructors
Each class has a single static constructor. This is a predefined, public, static method of the CLASS_CONSTRUCTOR class. If you want to use the static constructor, you must declare the static method CLASS_CONSTRUCTOR in the public section of the declaration part of the class using the CLASS-METHODS statement, and implement it in the implementation part. The static constructor has no interface parameters and cannot trigger exceptions. Unless you implement it explicitly it is merely an empty method.
The static constructor is executed once in each program. It is called automatically for the class class before the class is accessed for the first time - that is, before one of the following actions:
Generating an instance of a class using CREATE OBJECT obj, where obj has the data type REF TO class.
Calling a static method using [CALL METHOD] class=>meth.
Registering a static event handler method using SET HANDLER class=>meth for obj.
Registering an event handler method for a static event of the class class.
Addressing a static attribute with class=>a.
The static constructor is always called immediately before the action is executed, with one exception: If your first access to the class is to address a static attribute, the static constructor is executed at the beginning of the processing block (dialog module, event block, procedure) in which the access occurs.
Caution: The static constructor must not access its own class. Otherwise a runtime error will occur.
Caution: The point at which the static constructor is executed has not yet been finalized, and it may yet be changed. SAP only guarantees that it will be executed before the class is accessed for the first time. You are recommended not to write programs that require the static constructor to be executed at the beginning of a processing block.
Regards,
Santosh
‎2007 Jun 25 9:42 AM
‎2007 Jun 25 10:04 AM
Hello Kumar
Have a look at the following sample reports:
In order to display ALV grids we need a container instance. I prefer to use docking containers for this purpose.
Having instantiated the docking container (using its CONSTRUCTOR method) I create the ALV grid instance (again using its CONSTRUCTOR method).
Classes are not obliged to have a CONSTRUCTOR method yet they still can be instantiated. In this case the implicit CONSTRUCTOR method of the "root" class OBJECT is called.
Regards
Uwe
‎2007 Jun 25 10:59 AM
Hi Tirunagari,
<u><b>Constructor</b></u>
Constructor is a method in a class that is called implicitly at runtime
whenever a class is instantiated using the CREATE OBJECT statement.
It is an instance method and always named CONSTRUCTOR.
Each class can have only 1 constructor.
Constructor must be defined in the public area.
Constructors signature can have only importing parameters and
exceptions.
When exceptions are raised in constructor, instances are not created yet,
so no main memory space is occupied.
Except for one exceptional case (calling superclass constructor from
subclass) constructor cannot be called explicitly.
<u><b>USE of CONSTRUCTOR</b></u>
1. Initialize attributes
2. Modify static attributes
3. Send message containing the information that a new object was created
<u><b>CLASS CONTAINER</b></u>
<u><b>Sample program</b></u>
&----
Global data definitions for ALV
&----
&----
Object reference
&----
ALV Grid instance reference
DATA: gr_alvgrid TYPE REF TO cl_gui_alv_grid,
Custom container instance reference
gr_ccontainer TYPE REF TO cl_gui_custom_container.
&----
Internal Table
&----
Field catalog table
DATA: gt_fieldcat TYPE lvc_t_fcat,
Internal table holding list data
gt_list TYPE STANDARD TABLE OF sflight.
&----
Work area
&----
Layout structure
DATA: gs_layout TYPE lvc_s_layo,
Field catalog structure
gs_fcat TYPE lvc_s_fcat.
&----
Variables
&----
DATA: ok_code TYPE sy-ucomm,
save_ok TYPE sy-ucomm,
Name of the custom control added on the screen
gv_custom_control_name TYPE scrfname VALUE 'CC_ALV'.
&----
Calling the screen where ALV output is displayed
&----
CALL SCREEN 100.
&----
*& Module STATUS_0100 OUTPUT
&----
PBO
----
MODULE status_0100 OUTPUT.
PF Status
SET PF-STATUS 'MENU'.
ALV display
PERFORM display_alv.
ENDMODULE. " STATUS_0100 OUTPUT
&----
*& Module USER_COMMAND_0100 INPUT
&----
PAI
----
MODULE user_command_0100 INPUT.
save_ok = ok_code.
CLEAR ok_code.
IF save_ok EQ 'EXIT'.
LEAVE PROGRAM.
ENDIF.
ENDMODULE. " USER_COMMAND_0100 INPUT
&----
*& Form display_alv
&----
ALV display
----
FORM display_alv.
PERFORM get_data.
PERFORM create_alv.
ENDFORM. " display_alv
&----
*& Form get_data
&----
Fetch data to be displayed in the list
----
FORM get_data.
SELECT * FROM sflight
INTO TABLE gt_list.
ENDFORM. " get_data
&----
*& Form create_alv
&----
Create and set or Refresh ALV
----
FORM create_alv.
Checking whether an instance of the container (or ALV Grid) exists.
IF gr_alvgrid IS INITIAL.
If not, creating and setting ALV for the first display.
Creating custom container instance
CREATE OBJECT gr_ccontainer
EXPORTING
container_name = gv_custom_control_name
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
OTHERS = 6.
Creating ALV Grid instance
CREATE OBJECT gr_alvgrid
EXPORTING
i_parent = gr_ccontainer
EXCEPTIONS
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
OTHERS = 5.
Preparing field catalog.
PERFORM prepare_field_catalog CHANGING gt_fieldcat.
Preparing layout structure
PERFORM prepare_layout CHANGING gs_layout.
Method to display ALV grid
CALL METHOD gr_alvgrid->set_table_for_first_display
EXPORTING
is_layout = gs_layout
CHANGING
it_outtab = gt_list
it_fieldcatalog = gt_fieldcat
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
ELSE.
If an instance of the container (or ALV Grid) exists, refreshing it.
CALL METHOD gr_alvgrid->refresh_table_display
EXCEPTIONS
finished = 1
OTHERS = 2.
ENDIF.
ENDFORM. " create_alv
&----
*& Form prepare_field_catalog
&----
Subroutine to populate field catalog
----
<--P_GT_FIELDCAT Table to describe the field catalog
----
FORM prepare_field_catalog CHANGING p_gt_fieldcat TYPE lvc_t_fcat.
Generating the field catalog semi automatically
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
ct_fieldcat = p_gt_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
LOOP AT p_gt_fieldcat INTO gs_fcat.
CASE gs_fcat-fieldname.
WHEN 'CARRID'.
gs_fcat-outputlen = '10'.
gs_fcat-coltext = 'Airline Carrier ID'.
MODIFY p_gt_fieldcat FROM gs_fcat.
WHEN 'FLDATE'.
gs_fcat-just = 'C'.
gs_fcat-hotspot = 'X'.
MODIFY p_gt_fieldcat FROM gs_fcat.
ENDCASE.
ENDLOOP.
ENDFORM. " prepare_field_catalog
&----
*& Form prepare_layout
&----
Preparing layout structure
----
<--P_GS_LAYOUT Layout structure
----
FORM prepare_layout CHANGING p_gs_layout TYPE lvc_s_layo.
p_gs_layout-zebra = 'X' .
p_gs_layout-grid_title = 'Flight Info System'.
p_gs_layout-smalltitle = 'X'.
ENDFORM. " prepare_layout
Award points if found useful.
Regards
Indrajit.
‎2007 Jun 25 2:33 PM
hi
copy paste the code and create a CUSTOM CONTROL in a screen layout in se51
and give the name to container as CC_ALV
&----
*& Report ZTESTDEMO_INTERACTIVE_LIST_2
*&
&----
*&
*&
&----
REPORT ZTESTDEMO_INTERACTIVE_LIST_2.
TABLES: MARA,MARC,MARD.
internal table itab_mara 3 fields matnr, ernam,mtart
DATA: BEGIN OF ITAB_MARA OCCURS 0,
MATNR LIKE MARA-MATNR, " material number
ERNAM LIKE MARA-ERNAM, " name of person who create
MTART LIKE MARA-MTART, " Material Type
END OF ITAB_MARA.
internal table itab_marc 3 fields matnr, werks,lvorm
DATA: BEGIN OF ITAB_MARC OCCURS 0,
MATNR LIKE MARC-MATNR,
WERKS LIKE MARC-WERKS, " Plant
LVORM LIKE MARC-LVORM, " Flag Material for Deletion at Plant Level
END OF ITAB_MARC.
internal table itab_mard 2 fields
DATA: BEGIN OF ITAB_MARD OCCURS 0,
MATNR LIKE MARD-MATNR,
LGORT LIKE MARD-LGORT, " Storage Location
END OF ITAB_MARD.
SELECT-OPTIONS: S_MTART FOR MARA-MTART.
INITIALIZATION.
S_MTART-LOW = 'HALB'.
S_MTART-HIGH = 'HAWA'.
S_MTART-OPTION = 'BT'.
APPEND S_MTART.
START-OF-SELECTION.
SELECT MATNR ERNAM MTART FROM MARA INTO TABLE ITAB_MARA WHERE MTART IN
S_MTART.
PERFORM DISPLAY.
TOP-OF-PAGE.
WRITE:/2(15) 'MATERIAL NO',20(20) 'CREATED BY',45(15) 'MATERIAL TYPE'.
FORM DISPLAY.
LOOP AT ITAB_MARA.
WRITE:/ ITAB_MARA-MATNR UNDER 'MATERIAL NO' HOTSPOT ON,ITAB_MARA-ERNAM
UNDER 'CREATED BY',ITAB_MARA-MTART UNDER 'MATERIAL TYPE'.
HIDE: ITAB_MARA-MATNR.
ENDLOOP.
ENDFORM.
AT LINE-SELECTION.
CASE SY-LSIND.
WHEN 1.
SELECT MATNR WERKS LVORM FROM MARC INTO TABLE ITAB_MARC WHERE MATNR =
ITAB_MARA-MATNR.
PERFORM DISPLAY1.
WHEN 2.
SELECT MATNR LGORT FROM MARD INTO TABLE ITAB_MARD WHERE MATNR =
ITAB_MARC-MATNR.
PERFORM DISPLAY2.
when 3.
sy-lsind = 0.
ENDCASE.
FORM DISPLAY1.
LOOP AT ITAB_MARC.
WRITE:/ ITAB_MARC-MATNR HOTSPOT ON, ITAB_MARC-WERKS,ITAB_MARC-LVORM.
HIDE: ITAB_MARC-MATNR.
ENDLOOP.
WRITE:/ SY-LSIND.
ENDFORM.
FORM DISPLAY2.
LOOP AT ITAB_MARD.
WRITE:/ ITAB_MARD-MATNR, ITAB_MARD-LGORT.
ENDLOOP.
WRITE:/ SY-LSIND.
ENDFORM.
regards
ravish
<b>plz dont forget to reward points if helpful</b>
‎2007 Jun 27 6:22 PM
Simple answer: Constructor is used to instantiate a class. Values are set to class attributes in constructor which instantiates the class.