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. 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


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 statements is closed or after the analysis of a relational expression once the logical value has been 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 class (derived implicitly from the static type of oref).

CLASS class DEFINITION. 
  ... 
ENDCLASS. 

DATA: dref TYPE REF TO data, 
      oref TYPE REF TO class. 

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