‎2007 Jul 06 7:21 AM
when the instance of a class is created.does the instance contain static members of the class.
‎2007 Jul 06 8:02 AM
Hi Sandeep,
The instance of the class will definitely have the static components of the class, but the static component will not be specific to that instant.
Suppose you have a static component "count" in a class C1. You create instances Obj1, Obj2 of the class and in the constructor method increment
the static component "count".
So when you create first instant "Obj1", constructor will be called and count will be set to 1.
When you create next object "Obj2", again constructor will be called and count will be incremented from 1 (set to 1 first time) to 2. So basically the static component retains its value for all instances.
Please refer to following program for example.
&----
*& Class (Definiton) zl_lcl_vehicle
&----
Definition of Vehicle class
----
CLASS zl_lcl_vehicle DEFINITION.
PUBLIC SECTION.
Instance methods
METHODS: set_make IMPORTING value(im_make) TYPE string
im_model TYPE string,
get_make EXPORTING value(ex_make) TYPE string
ex_model TYPE string,
&----
Begin of IC011206
Functional Methods
Have a returning parameter and no exporting/changing parameters
The returning parameter must always be passed by value.
get_average_fuel IMPORTING im_distance TYPE i
im_fuel TYPE i
RETURNING value(re_fuel) TYPE i.
Static methods
CLASS-METHODS get_n_o_vehicles RETURNING value(re_count) TYPE i.
Static attribute
Can be accessed using classname and static method
CLASS-DATA gv_n_o_vehicles_pub TYPE i.
End of IC011206
&----
PRIVATE SECTION.
&----
Private Methods (Used for internal modularisation)
METHODS init_type. "IC011206+
&----
Instance attributes
DATA: gv_make TYPE string, " Vehicle make
gv_model TYPE string, " Type or model
gv_distance TYPE i,
" Distance IC011206+
gv_fuel TYPE i.
" Fuel IC011206+
&----
Static attribute
Can only be accessed using a static method
CLASS-DATA gv_n_o_vehicles_prv TYPE i. "IC011206+
&----
ENDCLASS. "zl_lcl_vehicle DEFINITION
&----
*& Class (Implementation) zl_lcl_vehicle
&----
Implementation of Vehicle class
----
CLASS zl_lcl_vehicle IMPLEMENTATION.
METHOD set_make.
IF im_make IS NOT INITIAL
AND im_model IS NOT INITIAL.
gv_make = im_make.
gv_model = im_model.
&----
Begin of IC011206
ELSE.
For calling an instance method from another method there is no need of
instance name. The method is automatically executed for the current
object
CALL METHOD init_type.
End of IC011206
&----
ENDIF.
ENDMETHOD. "set_make
METHOD get_make.
ex_make = gv_make.
ex_model = gv_model.
ENDMETHOD. "get_make
&----
Begin of IC011206
Functional Method
METHOD get_average_fuel.
re_fuel = im_fuel / im_distance.
ENDMETHOD. "get_average_fuel.
Static Method
METHOD get_n_o_vehicles.
Since this is a static method it can only access static components
and do not need instances, it can be directly accessed through the
class
gv_n_o_vehicles_prv = gv_n_o_vehicles_prv + 1.
re_count = gv_n_o_vehicles_prv.
gv_n_o_vehicles_pub = gv_n_o_vehicles_prv.
ENDMETHOD. "get_n_o_vehicles
Private Method
METHOD init_type.
CLEAR: gv_make,
gv_model.
ENDMETHOD. "init_type
End of IC011206
&----
ENDCLASS. "zl_lcl_vehicle
Declaring a reference variable with reference to the vehicle class.
This vareiable contains a reference to an object of theclass
DATA: z_vehicle1 TYPE REF TO zl_lcl_vehicle,
z_vehicle2 TYPE REF TO zl_lcl_vehicle. "IC011206+
DATA: gv_veh_make TYPE string,
gv_veh_model TYPE string,
gv_veh_count_prv TYPE i, "IC011206+
gv_veh_count_pub TYPE i, "IC011206+
gv_avg_fuel TYPE i. "IC011206+
START-OF-SELECTION.
Create instance of the vehicle class
CREATE OBJECT: z_vehicle1,
z_vehicle2. "IC011206+
Normal Syntax for method calls
CALL METHOD: z_vehicle1->set_make EXPORTING im_make = 'HYUNDAI'
im_model = 'SANTRO'.
*Additioanl Shorter Syntax for method calls. There must be no space
*before the bracket
and there must be at least one space after the bracket
z_vehicle1->get_make( IMPORTING ex_make = gv_veh_make
ex_model = gv_veh_model ).
&----
Begin of IC011206
Calling Functional methods of a class
gv_avg_fuel = z_vehicle1->get_average_fuel( im_distance = 5 im_fuel =
10 )
+ z_vehicle2->get_average_fuel( im_distance = 10 im_fuel =
20 ).
Call Static methods.
*Static methods can be addressed by class name and do not need
*instances.
*When called from another method in the same class, the classname can be
omitted.
zl_lcl_vehicle=>get_n_o_vehicles( RECEIVING re_count =
gv_veh_count_prv ).
Accessing Static attributes using class name. For this the static
attribute has to be defined in public section.
gv_veh_count_pub = zl_lcl_vehicle=>gv_n_o_vehicles_pub.
End of IC011206
&----
WRITE: / 'Make :', gv_veh_make,
/ 'Model :', gv_veh_model,
/ 'Average Fuel :', gv_avg_fuel, "IC011206+
/ 'Private Static attribute : no of vehicle =',
gv_veh_count_prv,"IC011206+
/ 'Public Static attribute : no of vehicle =', gv_veh_count_pub
."IC011206+
<b>Award points if found useful.</b>
Regards
Indrajit
‎2007 Jul 06 12:29 PM
Hi,
short answer is YES. This is part of OO concept.
BR, Artem
remeber points