SAP NEW CONSTRUCTOR PARAMS CLASS



Get Example source ABAP code based on a different SAP table
  



NEW, Classes

ABAP_SYNTAX
... NEW class$|#( $[let_exp$] p1 = a1 p2 = a2 ... ) ...

What does it do?
If a class class is specified or # stands for a class like this, the non-optional input parameters p1 p2 ... of the instance constructor of the instantiated class must be filled with the type-compliant actual parameters a1 a2 ... The optional input parameters do not need to but can be filled. The description of the statement CREATE OBJECT specifies how parameters are passed and which classes can be used. No parameters are passed for a class without an explicit instance constructor. An optional LET expression let_exp can be specified in front of the parameter to define local helper fields.
Like an object reference variable of the type class, a constructor expression NEW class( ... ) can be specified in front of the object component selector -> and in chained attribute accesses and chained method calls.
... NEW class( ... )->attr$|('attr_name') ...
... NEW class( ... )->meth( ... ) ...
An attribute can be specified statically as attr or dynamically as content of a character-like data object attr_name in parentheses. If a dynamically specified attribute is not found, an exception of class CX_SY_ASSIGN_ILLEGAL_COMPONENT is raised. A method meth can be specified statically only.
A chained method call of this type can be specified as a standalone statement or a functional method call. The result is then preserved for the duration of the statement. Even a single expression without a subsequent object component selector can be executed as a standalone statement. In this case, only the constructor that has a reference to the created object elsewhere is executed if this object needs to be preserved.
In the case of an attribute access NEW class( ... )->attr , the following applies specifically:
It can be specified as a writable expression in a result position.
If the data type is suitable, an offset/length specification +off(len) can be appended unless the chaining is used as memory area in the statement ASSIGN.
Method chainings cannot be specified between the constructor expression and the attribute here.



Latest notes:

This is a functional call of the instance constructor. As in functional calls of functional methods, no non-class-based exceptions can be handled.
When the instance constructor is called, the return code sy-subrc is set to 0. Values other than 0 are not possible, since non-class-based exceptions cannot be handled.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Construction of an instance of the class c1 when the method m2 of the class c2 is called, which requires an input parameter of this type.
ABEXA 01158
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
Use of a constructor expression with NEW in various positions. The temporary reference variable created using NEW is persisted during each statement. The example also shows dynamic specifications and the exception that is raised in case of an invalid attribute.
ABEXA 01159
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
Display of a time stamp created in a constructor that only exists while the statement is being executed.
ABEXA 01160
ABAP_EXAMPLE_END

ABAP_EXAMPLE_ABEXA
Instance Operator NEW on the Left
ABAP_EXAMPLE_END