Object Oriented ABAP Local Class Instance Attributes, Static Attributes and Constants

In this article we will discuss about Instance Attributes, Static Attributes and Constants in a local ABAP class.

Attributes are class variables. Attributes define in class are used to store data in the object class.

*SYNTAX TO DEFINE INSTACE ATTRIBUE, STATIC ATTRIBUE AND CONSTANTS
*Use DATA keyword to define Instance attributes
*Use CLASS-DATA to define Static attributes
*Use COSNTANTS keyword to define constants

CLASS <classname> DEFINITION.
PUBLIC SECTION.

DATA : ...

CLASS-DATA : ...

CONSTANTS : ...

ENDCLASS.

Object oriented ABAP supports Instance Attributes and Static Attributes.

Instance Attributes are created for each instance of the class. They are object specific and are created separately for each instance of the class. Instance attributes are defined using statement DATA.

Static Attributes are not instance specific and exist only once for all instance of the class i.e they exist once per class. Static attributes are defined using statement CLASS-DATA.

CONSTANTS value cannot be changed. Use CONSTANTS to store data that should not be changed both from inside the class as well as from outside the class.

Defining attributes with READ-ONLY addition – An attribute can have READ-ONLY addition only if it defined in the PUBLIC SECTION of the class. Define an attribute with READ-ONLY if you want to the attribute to be accessed from outside the class but cannot be modified from outside the class. An attribute with READ-ONLY addition can only be modified by a method of the class.