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

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

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

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