‎2008 Sep 03 4:24 PM
Hi all
can any one please let me know the read-only key word while declaring the public attributes?
Ur explanations are graeatly welcome instead of links.
Thanks
‎2008 Sep 03 4:32 PM
Since your attribute is public, you can access - Read and UPDATE - it.
Now, if you don't want the external application to modify the content of the attribute, you have to use the READ-ONLY.
CLASS LCL_TEST DEFINITION.
PUBLIC SECTION .
DATA: P_NUM1 TYPE I READ-ONLY,
P_NUM2 TYPE I.
METHODS: SET_VALUE.
ENDCLASS. "lcl_test DEFINITION
CLASS LCL_TEST IMPLEMENTATION.
METHOD SET_VALUE.
P_NUM1 = 10.
P_NUM2 = 20.
ENDMETHOD. "set_value
ENDCLASS. "lcl_test IMPLEMENTATION
START-OF-SELECTION.
DATA: O_TEST TYPE REF TO LCL_TEST.
CREATE OBJECT O_TEST.
CALL METHOD O_TEST->SET_VALUE.
WRITE: / O_TEST->P_NUM1,
/ O_TEST->P_NUM2.
** o_Test->p_num1 = 30. " This gives the Syntax error
* Write access to the READ-ONLY attribute "P_NUM1" is not allowed outside
* the class. outside the class.
O_TEST->P_NUM2 = 40.
WRITE: / O_TEST->P_NUM1,
/ O_TEST->P_NUM2.
Regards,
Naimesh Patel
‎2008 Sep 03 7:57 PM
Hello Radhu
Quite often you will find ABAP classes where key instance attributes are public yet read-only. Examples:
IF_PT_EMPLOYEE~PERNR of CL_PT_EMPLOYEE
IF_RECN_CONTRACT~MS_DETAIL of CL_RECN_CONTRACTAdvantage is that you do not need an explicit GET method to retrieve these key attributes.
Regards
Uwe
‎2008 Sep 03 8:02 PM
DATA : var type <type> read-only.
The READ_ONLY addition means that a public attribute declared with DATA can br read from outside but can only be changed by methods of the class.
Note : You can currently use READ-ONLY in public visibility section of class declartion.
Hope this helps.
‎2008 Sep 04 3:49 AM
Hi Shankar,
The Read-Only property specified for Public attributes is to provide the restrictions or to specify
the restrictions such that that particular method cannot be modified.. In general the Read-only
property is set if and only if the method is used particularly for Retrieving data or for any other
Retrieval process such that no other class or method cannot use that or change the nature of
that method.
Read-only properties are only set if the nature of the method should not be changed and also if
it is only used for retrieving data..
Hope this would help you.
Good luck
Narin
‎2008 Sep 04 5:39 AM
‎2008 Sep 04 5:48 AM
Hi,
If a public attribute is declared with READ ONLY addition. Then only methods of that class can change this attribute.
It can only be read from outside.
Regards
Abhijeet
‎2008 Sep 04 6:08 AM
hi,
By using Read only your are making your variable protective. By default Public variables are accessible from out side and they can also be updated from outside. By using read only option you can only read the data from outside but you can not modify the data.
Regards,
Naresh.