MichaelB.
7/25/2018 - 8:16 PM

Create an instance of a class (CREATE OBJECT)

When creating an object, you have to define an object reference variable. This is a pointer to an object of a class. It holds an object reference. Then you create the object, which is an instance of the class. You pass values to the Importing parameters of the constructor (if needed). We first have to tell the system where the program control flow should begin. Once you create the object the variable holds the actual program name and data to tell the system its holding an object reference to a certain class. The object also contains the attributes of the class, that still need to be set after creating the object.

When we have a constructor that requires parameters to be passed into it we have to specify these values that we want to pass. Do not have to specify the values for constructors that do not have import parameters.

* Object definitions using static type reference variables
...
START-OF-SELECTION.

DATA objectname TYPE REF TO classname.
    CREATE OBJECT objectname.

****************************************
* with constructor that contains importing parameters

* Object definitions
...
START-OF-SELECTION.

DATA objectname TYPE REF TO classname.
CREATE OBJECT objectname
        EXPORTING param1 = value1
                  param2 = value2.

*****************************************
*Create multiple instances

  DATA: car1 TYPE REF TO car,
        car2 TYPE REF TO car.

  CREATE OBJECT car1
    EXPORTING
      make     = 'AUDI'
      model    = 'A4'
      numseats = 5
      maxspeed = 120.


  CREATE OBJECT car2
    EXPORTING
      make     = 'BMW'
      model    = '20'
      numseats = 5
      maxspeed = 130.