Attributes of Classes
Attributes are internal data objects of any ABAP data type within a class. The content of the attributes specifies the state of the object. Reference variables can also be defined, which can then be used to create and address objects. This allows objects to be accessed within classes.
Attributes are defined in the declaration part of a class. Public attributes are visible from outside
the class without restrictions and as such are part of the interface between objects and their consumers.
To encapsulate the state of the object, protected, package-visible, or private attributes must be used.
The modifiability of non-private attributes can also be restricted using the addition
READ-ONLY in the declaration.
The content of instance attributes represents the instance-dependent state of the object. Instance attributes
are declared using the
COMMON PART cannot be used in classes.
The attempt to access an instance attribute via an initial object reference variable, in contrast to the call of an instance method, results in an exception that is not catchable.
The content of static attributes represents the non-instance-independent state of the object, which
is valid for all instances of the class. Static attributes exist once for each class. They are declared
using the statement
and are preserved throughout the entire runtime. All the objects within a class can access its static attributes. Changes to a static attribute in an object are visible to all other objects within that class.
Data Types of Attributes
The data types of all attributes, including the instance attributes and in particular the
bound data types, are
part of the static properties of a class. Therefore, in a
the class component selector or reference variables can be used to reference visible attributes of a
class, without first creating an object. In this way, the public static attributes of global classes can be accessed in each program.
References the data type of an instance attribute
attr of a global class
DATA dref TYPE REF TO cl_global.
DATA: f1 LIKE cl_global=>attr,
f2 LIKE dref->attr.
Like substructures of nested structures, attributes declared as structures can be declared as
static boxes using the addition
BOXED. In static boxes,
initial value sharing causes less memory to be used for little used structures of frequently used objects. A static box used as a
boxed component is a deep component managed using an internal reference, like strings and internal tables.