Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

creating object: obj1 EXPORTING id = '...'

Former Member
0 Likes
2,688

I have a doubt, that meaning has the next code?:

CREATE OBJECT:

account1 EXPORTING id = `...`.

overwrite the constructor of object account1, that have the parameter id, passing the value '...' ?

cordial greetings.

1 ACCEPTED SOLUTION
Read only

marcelo_ramos1
SAP Mentor
SAP Mentor
0 Likes
1,070

Hi Lopes,

See the follow example.


REPORT  z_mar_demo.

*----------------------------------------------------------------------*
*       CLASS ONE DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one DEFINITION.

  PUBLIC SECTION.
"   The constructor method must be definited on Public Section
"   Here we're definig a constructor method without parameters.
    METHODS constructor.

  PRIVATE SECTION.

    DATA attribute TYPE char10.

ENDCLASS.                    "ONE DEFINITION


*----------------------------------------------------------------------*
*       CLASS one IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one IMPLEMENTATION.

  METHOD constructor.
"   Inside Construcot we can initialize attributes, call othres methods, etc...
    MOVE 'value X' TO attribute.

  ENDMETHOD.                    "CONSTRUCTOR

ENDCLASS.                    "one IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS two DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS two DEFINITION.

  PUBLIC SECTION.

"   Here we're definig a constructor method with a parameter.
"   Constructor can have only import parameters and its can be defineted
"   as Optional
    METHODS constructor IMPORTING value TYPE char10 OPTIONAL.

  PRIVATE SECTION.

    DATA attribute TYPE char10.

ENDCLASS.                    "two DEFINITION


*----------------------------------------------------------------------*
*       CLASS two IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS two IMPLEMENTATION.

  METHOD constructor.

    IF value IS NOT INITIAL.
      MOVE value TO attribute.
    ELSE.
      MOVE 'value Y' TO attribute.
    ENDIF.
  ENDMETHOD.                    "CONSTRUCTOR

ENDCLASS.                    "two IMPLEMENTATION

DATA o_one TYPE REF TO one.
DATA o_two TYPE REF TO two.
DATA o_two2 TYPE REF TO two.

START-OF-SELECTION.

" As Constructor of class one doesn't has parameters we can create its object as follow
  CREATE OBJECT o_one.

" As parameter was definited as Optional we can pass the parameter during object instantiation.
  CREATE OBJECT o_two
    EXPORTING
      value = 'Value Y'.


" Or we can just pass a null value Like value = ' '. or value = SPACE.  
  CREATE OBJECT o_two
    EXPORTING
      value = ' '.

" As parameter was definited as Optional we can miss the parameter during object instantiation.
  CREATE OBJECT o_two2.

Regards.

Marcelo Ramos

3 REPLIES 3
Read only

uwe_schieferstein
Active Contributor
0 Likes
1,070

Hello Fernandez

The coding simply means:

- Create an instance account1 and set some default value ( id = '...').

Within the CONSTRUCTOR method you can see which instance attribute is filled with the contens of the formal parametr <i>id.</i>

Regards

Uwe

Read only

marcelo_ramos1
SAP Mentor
SAP Mentor
0 Likes
1,071

Hi Lopes,

See the follow example.


REPORT  z_mar_demo.

*----------------------------------------------------------------------*
*       CLASS ONE DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one DEFINITION.

  PUBLIC SECTION.
"   The constructor method must be definited on Public Section
"   Here we're definig a constructor method without parameters.
    METHODS constructor.

  PRIVATE SECTION.

    DATA attribute TYPE char10.

ENDCLASS.                    "ONE DEFINITION


*----------------------------------------------------------------------*
*       CLASS one IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one IMPLEMENTATION.

  METHOD constructor.
"   Inside Construcot we can initialize attributes, call othres methods, etc...
    MOVE 'value X' TO attribute.

  ENDMETHOD.                    "CONSTRUCTOR

ENDCLASS.                    "one IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS two DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS two DEFINITION.

  PUBLIC SECTION.

"   Here we're definig a constructor method with a parameter.
"   Constructor can have only import parameters and its can be defineted
"   as Optional
    METHODS constructor IMPORTING value TYPE char10 OPTIONAL.

  PRIVATE SECTION.

    DATA attribute TYPE char10.

ENDCLASS.                    "two DEFINITION


*----------------------------------------------------------------------*
*       CLASS two IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS two IMPLEMENTATION.

  METHOD constructor.

    IF value IS NOT INITIAL.
      MOVE value TO attribute.
    ELSE.
      MOVE 'value Y' TO attribute.
    ENDIF.
  ENDMETHOD.                    "CONSTRUCTOR

ENDCLASS.                    "two IMPLEMENTATION

DATA o_one TYPE REF TO one.
DATA o_two TYPE REF TO two.
DATA o_two2 TYPE REF TO two.

START-OF-SELECTION.

" As Constructor of class one doesn't has parameters we can create its object as follow
  CREATE OBJECT o_one.

" As parameter was definited as Optional we can pass the parameter during object instantiation.
  CREATE OBJECT o_two
    EXPORTING
      value = 'Value Y'.


" Or we can just pass a null value Like value = ' '. or value = SPACE.  
  CREATE OBJECT o_two
    EXPORTING
      value = ' '.

" As parameter was definited as Optional we can miss the parameter during object instantiation.
  CREATE OBJECT o_two2.

Regards.

Marcelo Ramos

Read only

Former Member
0 Likes
1,070

Hi

The CREATE OBJECT statement creates an object in the main memory. The attribute values of this object are either initial values or correspond to the VALUE entry.

Reference variables can also be assigned to each other.

For the above example this would mean that r_vehicle1 and r_vehicle2 point to the same object.