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:

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.

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.

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.

Note

From a technical perspective, the instance operator NEW 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.

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