Showing posts with label Components of classes. Show all posts
Showing posts with label Components of classes. Show all posts

Tuesday, June 23, 2009

Data Types and Constants

Data Types

Independent Types

You can use the TYPES statement to define any number of your own ABAP data types within a class. Types are not instance-specific and are only available once for all the objects in the class.

In particular, it is possible to define data types in the public visibility section of global classes, which makes the use of type groups in this context obsolete.

Bound Data Types

Bound data types that occur as properties of instance or static attributes also belong to the static attributes of a class. After a LIKE addition, the class name can be used to access the properties of instance attributes (exceptions to this rule are the statements ASSIGN ... CASTING and SELECT-OPTIONS ... FOR). In addition, a reference variable can be used with an object component selector without the object having previously been generated.

Constants

Constants are special static attributes for which values are specified when they are declared. These values cannot be changed later. Use the CONSTANTS statement to declare constants. Constants are not instance-specific - they are only available once for all the objects in the class.

In particular, it is possible to declare constants in the public visibility section of global classes, which makes the use of type groups in this context obsolete.

Events In Components of classes

n ABAP Objects, events are declared as components of classes. SAP makes a distinction between instance events and static events. Triggers and handlers can be objects and classes, depending on whether they are instance events, static events, or event handlers.

Instance events

Instance events are declared using the EVENTS statement. They can only be triggered in instance methods.

Static events

Static events are declared using the CLASS-EVENTS statement. All methods (instance or static) can trigger them, but only static events can be triggered by static methods.

Constructors In Components of Classes

Constructors are special methods that produce a defined initial state for objects and classes. The state of an object is determined by its instance attributes and static attributes. You can assign contents to attributes using the VALUE addition in the DATA statement. Constructors are necessary when you want to set the initial state of an object dynamically.

Like normal methods, there are two types of constructor - instance constructors and static constructors.

For inheritance, some special rules apply to constructors that are not described here. These are covered in more detail in the inheritance context.

Instance Constructors

Each class has one instance constructor. It is a predefined instance method of the CONSTRUCTOR class. If you want to use the instance constructor, the constructor method must be declared in a visibility area of the class using the METHODS statement, and implemented in the implementation section. The visibility area in which the instance constructor can be declared, must be more general or equal to the instantiability defined by the addition CREATE of the CLASS statement, where the most specialized area is recommended. Unless it is explicitly declared, the instance constructor is an implicit method, which inherits and accesses the interface from the instance constructor in the upper class.

The declaration is carried out in a visibility area of the declaration part due to technical reasons. The actual visibility of the instance constructor is controlled by the CREATE PUBLIC|PROTECTED|PRIVATE additions of the CLASS statement. For more information refer to Visibility of Instance Constructors.

Instance constructors are called once for each instance. They are called automatically, immediately after you have created an instance using the CREATE OBJECT statement. It is not possible to call an instance constructor directly using the CALL METHOD statement.

An instance constructor can contain an interface with IMPORTING parameters and exceptions. You define the interface using the same syntax as for normal methods in the METHODS statement. The fact that there are no exporting parameters shows that constructors serve only to define the state of an object and have no other function. To transfer parameters and handle exceptions, use the EXPORTING and EXCEPTIONS additions to the CREATE OBJECT statement.

Static Constructors

Each class has a single static constructor. This is a predefined, public, static method of the class named constructor. If you want to use the static constructor, you must declare the static method class_constructor in the public section of the declaration part of the class using the CLASS-METHODS statement, and implement it in the implementation part. The static constructor has no interface parameters and cannot trigger exceptions. Unless you declare it explicitly, it is merely an empty method.

The static constructor is called once per class and internal session. It is called automatically for the class class before the class is accessed for the first time - that is, before one of the following actions:

  • Generating an instance of a class using CREATE OBJECT obj, where obj has the data type REF TO class.
  • Calling a static method using [CALL METHOD] class=>meth.
  • Registering a static event handler using SET HANDLER class=>meth for obj.
  • Registering an event handler for a static event of the class class.
  • Addressing a static attribute with class=>a.

The static constructor is always called immediately before the action is executed, with one exception: If your first access to the class is to address a static attribute, the static constructor is executed at the beginning of the processing block (dialog module, event block, procedure) in which access takes place.

Notes

  • The point at which the static constructor is called has not yet been finalized. SAP only guarantees that it will be called before the class is accessed for the first time. For this reason, static methods may be executed before the static constructor was ended.
  • The execution sequence of static constructors is dependent on the program flow. Static constructors must be implemented in such a way that they can be executed in any sequence.

Methods in Components of classes

Methods are internal procedures of a class that determine the behavior of an object. They can access all the attributes of their class and can thus change the object status. Methods have a parameter interface, through which the system passes values to them when they are called, and through which they can return values to the caller. The private attributes of a class can only be changed using methods.

A method meth is declared in the declaration section of a class and in the implementation part of the class using the processing block.

METHOD meth.
...
ENDMETHOD.

You can declare local data types and data objects in methods, just as in all procedures. Methods are called using the CALL METHOD statement or one of its abbreviated forms. You can also call the method dynamically (dynamic invoke).

Instance Methods

Instance methods are declared using the METHODS statement. They can access all the attributes of a class and can trigger all its events.

Static Methods

Static methods are declared using the CLASS-METHODS statement. They can access static attributes of a class and are only allowed to trigger static events.

Constructors

As well as the normal methods that are called using CALL METHOD, there are two special methods called constructor and class_constructor, which are called implicitly when an object is created or when a class component is accessed for the first time.

Functional Methods

Functional methods are methods with any number of IMPORTING parameters and one RETURNING parameter. Functional methods cannot only be called using CALL METHOD. The following expressions can also be used to insert them at operand positions for functions and expressions:

  • Functional method without IMPORTING parameters:

    meth( )
  • Functional method with a non-optional IMPORTING parameter or a number of optional IMPORTING parameters with a preferred parameter:

    meth( f1 )
  • Functional method with IMPORTING parameters:

    meth( p1 = f1 ... pn = fn )

At this operand position it is also possible to link function modules together to make method chains.



Attributes

Attributes are internal data objects of any ABAP data type within a class. The content of the attributes specifies the status of the object. You can also define reference variables, which you can then use 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 completely visible from outside the class and as such are part of the interface between objects and their user. To encapsulate the status of the object, you need to use protected or private attributes. You can also limit the changeability of non-private attributes using the READ-ONLY addition during the declaration.

Instance Attributes

The content of instance attributes forms the instance-specific status of the object. Instance attributes are declared using the DATA statement. You cannot use the COMMON PART addition in classes.

Static Attributes

The content of static attributes forms the instance-independent status of the object, which is valid for all instances of the class. Static attributes are available once for each class. They are declared using the CLASS-DATA statement and are retained 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 belong to the static properties of a class. Therefore, in a LIKE addition, you can use the class component selector or reference variables to refer to the visible attributes of a class without first creating an object. In this way, the access to the properties of the public static attributes of global classes is possible in every program.

Example

Reference to the data type of an instance attribute attr of a global class cl_global.

DATA dref TYPE REF TO cl_global.

DATA: f1 LIKE cl_global=>attr,
f2 LIKE dref->attr.

Boxed Components

Attributes declared as structures can be declared as boxed components using the addition BOXED, like substructures of nested structures. In boxed components, initial value sharingcauses less memory to be used for little used structures of much used objects. A boxed component is a deep component administered using an internal reference, like strings and internal tables.

Blog Archive