SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Declarations → Declaration Statements → Classes and Interfaces → Components in Classes and Interfaces → Methods → METHODS →
METHODS - constructor
Syntax
METHODS constructor [FINAL]
[IMPORTING parameters [PREFERRED PARAMETER p]]
[{RAISING exc1|RESUMABLE(exc1) exc2|RESUMABLE(exc2) ...}
|{EXCEPTIONS exc1 exc2 ...}].
Extras:
1. ... IMPORTING parameters
2. ... RAISING exc1|RESUMABLE(exc1) exc2|RESUMABLE(exc2) ...
3. ... EXCEPTIONS exc1 exc2 ...
4. ... FINAL
Effect
This statement declares the instance constructor constructor of a class. In a local class, it can be specified in all visibility sections that have more general instantiability than or the same instantiability specified in the CREATE addition of the statement CLASS DEFINITION:
CREATE PUBLIC | CREATE PROTECTED | CREATE PRIVATE | |
PUBLIC SECTION | X | X | X |
PROTECTED SECTION | - | X | X |
PRIVATE SECTION | - | - | X |
In a global class, the instance constructor must always be declared in the public visibility section, for technical reasons. In principle, the constructor can be declared in the same visibility sections as in the table above, however this can cause unexpected syntax errors when the class is used, due to the way it is organized internally.
Each class has a predefined method called constructor. By declaring this explicitly, the interface of the method constructor can be defined specifically for a class, and its functions can be implemented. Without explicit declaration, the instance constructor assumes the parameter interface of the direct superclass, and calls it implicitly.
If the instance constructor is implemented in a subclass, the instance constructor of the superclass must be called explicitly using super->constructor, even if the latter is not explicitly declared. The only exceptions to this are direct subclasses of the root node object. The following restrictions apply before the superclass constructor is called:
After the superclass constructure has been called, the self-reference me-> can be used and instance components can be accessed.
For each instance of a class, the instance constructor is called only once using the statement CREATE OBJECT immediately after it has been generated. For the call, appropriate actual parameters must be assigned to all non-optional input parameters, return codes can be assigned to non-class-based exceptions, and class-based exceptions can be declared. It is not possible to call the instance constructor explicitly, except when calling the superclass constructors using super->constructor in the redefined constructor of a subclass.
When an instance constructor is executed, the current instance temporarily assumes the type of the class in which the constructor is defined. This has the following consequences:
Programming Guideline
Declare the instance constructor in the public visibility section.
Notes
... IMPORTING parameters
... RAISING exc1|RESUMABLE(exc1) exc2|RESUMABLE(exc2) ...
... EXCEPTIONS exc1 exc2 ...
Effect
The IMPORTING addition can be used to define input parameters according to
the same rules as for general methods. The additions RAISING and EXCEPTIONS
for the declaration of class-based exceptions or the definition of non-class-based exceptions also have the same meaning as for general methods.
Note
When the instance constructor is executed, the same applies for
resumable exceptions
as for all other methods. If processing can be resumed successfully, the creation of the object can also be resumed.
... FINAL
Effect
Instance constructors are implicitly final. The addition FINAL can be specified, but it is not necessary.
Example
In this example, the class c2 inherits from the class c1. In both classes, the instance constructor constructor is declared explicitly. It must therefore be implemented in both classes, whereby the implementation in c2 must include the call of the superclass constructor.
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS constructor IMPORTING p1 TYPE any.
...
ENDCLASS.
CLASS c2 DEFINITION INHERITING FROM c1.
PUBLIC SECTION.
METHODS constructor IMPORTING p2 TYPE any.
...
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD constructor.
...
ENDMETHOD.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
METHOD constructor.
...
super->constructor( p2 ).
...
ENDMETHOD.
ENDCLASS.