SAP NetWeaver AS ABAP Release 750, ©Copyright 2016 SAP AG. All rights reserved.
ABAP - Keyword Documentation →
ABAP - Reference →
Creating Objects and Values →
NEW - Instance Operator
Syntax
... NEW type( ... ) ...
Effect
A constructor expression with the instance operator NEW creates an
anonymous data object or an
instance of a class. The result
is a reference variable that points to the new object. The following can be specified for type:
- A non-generic data type dtype.
The operator NEW
works in the same way as the statement CREATE
DATA dref TYPE dtype, where dref stands for the result that points to the new anonymous data object. The result is a data reference variable of the
static type dtype. A constructor expression of this type cannot be continued using a
component selector.
- A class class.
The operator NEW works
in the same way as the statement CREATE
OBJECT oref TYPE class, where oref stands for the result that points to the new object. The result is an object reference variable of the
static type class. A constructor expression of this type can be continued at
general expression positions and
functional positions, like an object reference
variable, using an object component selector ->, and can be used at the same operand positions. Furthermore,
- A single expression, which points to an attribute of the class using exactly one following object component selector, can also be used as the target field of assignments.
- The # character.
If the data type required in an operand position
is unique and fully recognizable, the # character can be used instead of an explicitly specified type type and the
operand type is used.
If the operand type is not fully identifiable, the character # cannot be used, with the following exception: The operand can be evaluated after BASE when a
structure or an internal table is constructed.
The same descriptions apply as for the CREATE statements. Once an object
has been created, it is provided with values using the parameters in parentheses. The syntax used in pass by parameter depends on the type used. There are specialized categories of pass by parameter for complex types.
When a constructor expression is assigned to a reference
variable using NEW, the information in the parentheses is evaluated before the new object is bound to the target variable.
Return Value
If an instance of a
class is created successfully, the instance
operator NEW sets sy-subrc to 0. Non-class-based
exceptions of the instance constructor cannot be handled, which means that sy-subrc
is never set to a value other than 0. The return code sy-subrc is not set when anonymous data objects are created.
Notes
- The instance operator NEW always creates a new temporary reference variable
that points to the new object. The reference variable is used as the operand of a statement and then
deleted. It is deleted when the current statement is closed or after the analysis of a relational expression once the truth value is determined. The new object is passed to the garbage collector if it is not passed to a
heap reference or a field symbol after the temporary reference variable is deleted.
- Assignments to a reference variable also always
create a temporary reference variable that is only assigned to the target variable afterwards. This
means that the object pointed to by the target variable before the assignment can be addressed using
it throughout the entire expression. This is the difference between NEW and
the value operator VALUE.
Example
Creates an anonymous data object of the type i with the value 555 and an
instance of a local class cls (derived implicitly from the static type of
oref). In this case, the last statement can be written just as explicitly as oref = NEW cls(
) or with inline declaration instead of the previous DATA statement as DATA(oref) = NEW cls( ).
CLASS cls DEFINITION.
...
ENDCLASS.
DATA: dref TYPE REF TO data,
oref TYPE REF TO cls.
dref = NEW i( 555 ).
oref = NEW #( ).
Continue
NEW - Initial Value for All Types
NEW - Single Value for All Data Types
NEW - Structures
NEW - Internal Tables
NEW - Classes