Summary -

In this topic, we described about the below sections -

Attributes are internal data fields within a class that can be declared with any ABAP data type such as C, I, F and N. The object state is determined by its attributes contents.

Attributes are two types and those are -

Attribute Types Description
Instance Attributes The instance attributes contents defines the instance-specific state of an object. The states are different for different objects. Instance attributes can be declared using the DATA statement.
Static Attributes The static attribute defines the state of the class. The states are common for all different object instances of the class. Static attributes exist only once. Static attributes can declare by using the CLASS-DATA statement. All the objects in a class can access its static attributes. If a static attribute in an object changed, the change is visible in all other objects of the class.

Example -

Simple example to define instance variable and static variables.


Code -

*&---------------------------------------------------------------------*
*& Report  Z_ATTRIBUTES
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_ATTRIBUTES.

* Class and Method definition
CLASS classnew DEFINITION.
PUBLIC SECTION.

* Defining instance variable inst_var
  DATA inst_var(30) TYPE C VALUE 'Instance Variable'.
  
* Definging static variable stat_var
  CLASS-DATA stat_var(30) TYPE C VALUE 'Static Variable'.
  
* Defining public method methodnew for the class classnew
  METHODS: methodnew.
ENDCLASS.

* Class & Method Implementation
CLASS classnew IMPLEMENTATION.

* Method implementation
  METHOD methodnew.

* Displays instance variable
     WRITE:/ inst_var.
     
* Displays static variable
     WRITE:/ stat_var.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

* Defining object of reference type to class.
DATA: objectnew TYPE REF TO classnew.

* Creatng class object
CREATE OBJECT objectnew.

* Call method using object.
CALL METHOD: objectnew->methodnew. 

Output -

Attributes Example Output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

inst_var is instance variable because it was defined by using DATA clause. Stat_var is static variable because it was defined by using CLASS-DATA clause.