Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Declarations →  Declaration Statements →  Data Types and Data Objects →  Declaring Data Objects →  DATA 

DATA - data_options

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... [ VALUE val|{IS INITIAL} ] 
    [ READ-ONLY ].

Extras

1. ... VALUE val|{IS INITIAL}

2. ... READ-ONLY

Effect

The additions VALUE and READ-ONLY are specifically for data objects. They distinguish the DATA syntax from the TYPES syntax.


Note

As well as the additions VALUE and READ-ONLY, the syntax also allows the obsolete addition COMMON PART.

Addition 1

... VALUE val|{IS INITIAL}

Effect

The addition VALUE can be used to define a start value val for the content of a variable for regardless of how the variable is declared. This value is used to initialize the variable if it is created before LOAD-OF-PROGRAM. The addition VALUE is not allowed in the declaration part of an interface in the statement DATA.

The start value val can either be specified as a literal or as a predefined constant. Where constants are involved, they work like the literal specified after VALUE when the constant is declared, rather than their actual values being used. A check is usually performed only to verify whether the length of the specified value matches the data type. Any deviations produce a warning from the syntax check. Only for time stamp type utclong does a character literal that contains a valid representation of a time stamp need to be specified. If the data type of the literal does not match the data type of the declaration, it is usually converted (when the program is generated and activated) in accordance with the conversion rules for elementary data types. If the literal cannot be converted to the data type, an exception is raised when the program is generated and there is no syntax error.

Without the addition VALUE, or if IS INITIAL is specified, the content is set to an initial value. The initial values are dependent on the data type.

In the case of initial structures, the components are initial, initial reference variables contain the null reference (which does not point to an object), and initial internal tables do not contain any rows.

The addition VALUE is possible for all data types, in particular for the deep types (strings, reference types, table types, or structured types with deep components, including boxed components). A start value val, however, can only be specified for the ABAP types string and xstring. Otherwise, only IS INITIAL is possible. IS INITIAL is also the only possible start value for structures with components that are not just character-like and flat.

Programming Guideline

Specifying Type-Friendly Start Values


Notes

  • A start value should be specified according to type. In particular, no values that are longer should be specified and, in the case of certain data types such as d and t, the length must match exactly.
  • If numbers with decimal places are specified or if an exponent is used as a start value for data objects of the data types p or f in scientific notation with mantissa, this means there are no literals for these numbers. Instead, the character literals must be specified with the appropriate content. These are then converted into the numeric data type in accordance with the conversion rules for elementary data types. The same applies to byte-like data objects.
  • If the data object is a character-like object, the enumeration constants val1, val2, ... of the value set of an enumerative type enum can also be specified for val. The constant is then given the name of the enumerative constant as its value.
  • The value operator VALUE can also be used to construct the content of complex data objects (structures, internal tables).

Example

Declares data by specifying the the initial value. One value is specified by a constant.

CONSTANTS underscores TYPE string VALUE `__________`. 

DATA: 
  num TYPE i       VALUE 123, 
  txt TYPE string  VALUE underscores, 
  hex TYPE xstring VALUE `0123456789ABCDE`.

Example

The following example demonstrates that, whenever a constant is used after VALUE, the literal specified after the VALUE addition of this constant is evaluated. The variable text1 is given the full content of the literal. The variable text2, on the other hand, is assigned the value of the constant that containing only the digits of the literal and any leading zeroes in accordance with the conversion rule from c to n.

CONSTANTS 
  const TYPE n LENGTH 10 VALUE 'x1x1x1x'. 

TYPES 
  text TYPE c LENGTH 10. 

DATA: 
  text1 type text VALUE const, 
  text2 TYPE text. 

text2 = const. 

cl_demo_output=>display( 
  |text1: { text1 }\ntext2: { text2 }| ). 

Addition 2

... READ-ONLY

Effect

This addition is always possible in the public visibility section of a class or in an interface. This addition makes an attribute declared using DATA readable from outside of the class, but can only be changed using methods of the class or its subclasses. This addition is ignored by the friends of the class.

A class attribute defined using READ-ONLY can be used outside of the class, its friends, and subclasses only in reading positions in ABAP statements.


Notes

  • The declaration of attributes using the addition READ-ONLY does not prevent methods of the class from passing references to these attributes externally as reference variables or field symbols and therefore making the attributes modifiable outside of the class.
  • The addition READ-ONLY is always recommended if attributes need to be invisible, but a GET method for every read is to be avoided.

Example

Uses the static constructor as the factory method of a class with instances created privately. The reference to the Singleton objects created is available in the READ-ONLY attribute clsref.

CLASS cls DEFINITION CREATE PRIVATE. 
  PUBLIC SECTION. 
    CLASS-DATA clsref TYPE REF TO cls READ-ONLY. 
    CLASS-METHODS 
      class_constructor. 
    ... 
ENDCLASS. 

CLASS cls IMPLEMENTATION. 
  METHOD class_constructor. 
    IF clsref IS INITIAL. 
      clsref = NEW #( ). 
    ENDIF. 
  ENDMETHOD. 
  ... 
ENDCLASS.