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

Read-only addition while declaring the public attribute

radhushankar
Participant
0 Likes
1,306

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

7 REPLIES 7
Read only

naimesh_patel
Active Contributor
0 Likes
1,011

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

Read only

uwe_schieferstein
Active Contributor
0 Likes
1,011

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_CONTRACT

Advantage is that you do not need an explicit GET method to retrieve these key attributes.

Regards

Uwe

Read only

Former Member
0 Likes
1,011

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.

Read only

narin_nandivada3
Active Contributor
0 Likes
1,011

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

Read only

Former Member
0 Likes
1,011

This message was moderated.

Read only

Former Member
0 Likes
1,011

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

Read only

Former Member
0 Likes
1,011

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.