Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Creating Objects and Values →  NEW - Instance Operator 

NEW - Classes

Other versions: 7.31 | 7.40 | 7.54

Syntax


... NEW class|#( [let_exp] p1 = a1 p2 = a2 .. ) ...

Effect

If a class class is specified or # stands for a class like this, the non-optional input parameters p1 p2 ... of the instance constructor of the instantiated class must be filled using the type-compliant actual parameters a1 a2 .... The optional input parameters do not need to be filled. The description of the statement CREATE OBJECT specifies how values are passed by parameter and which classes can be used. No parameters are passed for a class without an explicit instance constructor A LET expression let_exp can be specified (optional) before the parameter to define local auxiliary fields.

Like an object reference variable of the type class, a constructor expression NEW class( ... ) can be specified before the object component selector -> and in chained attribute accesses and chained method calls. A chained method call of this type can be specified as a standalone statement. This persists the result for the duration of the statement. Even a single expression without a following object component selector can be specified as a standalone statement. In this case, the only constructor executed is the constructor that needs to save a reference to the new object elsewhere (if it needs to be persisted).

The following applies in particular to an attribute access NEW class( ... )->attr:

Method chainings cannot be specified between the constructor expression and the attribute here.


Notes

  • When the instance constructor is called, the return code sy-subrc is set to 0. Values other than 0 are not possible, since non-class-based exceptions cannot be handled.

Example

Constructs an instance of the class c1 when the method m2 of the class c2. Here, an input parameter of the type in question is expected.

CLASS c1 DEFINITION. 
  PUBLIC SECTION. 
    METHODS constructor IMPORTING p1 TYPE i p2 TYPE i. 
ENDCLASS. 

CLASS c1 IMPLEMENTATION. 
  METHOD constructor. 
    ... 
  ENDMETHOD. 
ENDCLASS. 

CLASS c2 DEFINITION. 
  PUBLIC SECTION. 
    CLASS-METHODS m2 IMPORTING oref TYPE REF TO c1. 
ENDCLASS. 

CLASS c2 IMPLEMENTATION. 
  METHOD m2. 
    ... 
  ENDMETHOD. 
ENDCLASS. 

START-OF-SELECTION. 

  c2=>m2( NEW #( p1 = 1 p2 = 2 ) ).

Example

Uses a constructor expression with NEW in various positions. The temporary reference variable created using NEW is persisted during each statement.

CLASS c1 DEFINITION. 
  PUBLIC SECTION. 
    DATA     a1 TYPE i. 
    METHODS: m1 RETURNING value(p) TYPE i, 
             m2. 
ENDCLASS. 

CLASS c1 IMPLEMENTATION. 
  METHOD m1. 
    ... 
  ENDMETHOD. 
  METHOD m2. 
    ... 
  ENDMETHOD. 
ENDCLASS. 

DATA num TYPE i. 

START-OF-SELECTION. 

  num = NEW c1( )->a1. 
  NEW c1( )->a1 = num. 
  num = NEW c1( )->m1( ). 
  NEW c1( )->m2( ). 
  NEW c1( ).

Example

Displays a time stamp created in a constructor that only exists while the statement is being executed.

CLASS utc DEFINITION. 
  PUBLIC SECTION. 
    DATA ts TYPE timestamp. 
    METHODS constructor. 
ENDCLASS. 

CLASS utc IMPLEMENTATION. 
  METHOD constructor. 
    GET TIME STAMP FIELD ts. 
  ENDMETHOD. 
ENDCLASS. 

START-OF-SELECTION. 
  cl_demo_output=>display_text( 
    |{ NEW utc( )->ts TIMESTAMP = ISO }| ).

Example

Constructor expression with NEW on the left side of an expression. The temporary reference variable created using NEW exists only while the statement is being executed, however the reference to the object is assigned to the reference variable oref of the class class. This persists the object.

CLASS class DEFINITION. 
  PUBLIC SECTION. 
    DATA attr TYPE string. 
    METHODS constructor. 
ENDCLASS. 

DATA oref TYPE REF TO class. 

CLASS class IMPLEMENTATION. 
  METHOD constructor. 
    oref = me. 
  ENDMETHOD. 
ENDCLASS. 

START-OF-SELECTION. 
  NEW class( )->attr = 'foo'. 
  cl_demo_output=>display( oref->attr ).