Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Creating Objects and Values 

NEW - Instance Operator

Other versions: 7.31 | 7.40 | 7.54

Syntax


... NEW type( ... ) ...

Effect

A constructor expression with the instance operator NEW creates an anonymous data object or an instance of a class and assigns values to the new object. 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 extended 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. Using an object component selector ->, a constructor expression of this type can be extended in both general expression positions and functional positions (like an object reference variable) and can be used in the same operand positions. The following is also possible:
  • A single expression that points to an attribute of the class using exactly one follow-on 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 known in full, 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 to the CREATE statements. Once an object is created, it is provided with values using the parameters in parentheses. The syntax used in parameter passing depends on the type used. There are specialized categories of parameter passing 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


Notes

  • To create the values for a new data object, the instance operator NEW uses mainly the same syntax as the value operator VALUE.

  • 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 could be written just as explicitly as oref = NEW cls( ) or it could be written as DATA(oref) = NEW cls( ), using an inline declaration instead of the preceding DATA statement.

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