Creating Objects

In order to call a non-static method, an object is required. The class of the object must be the same as the class containing the method. You may already have an object available, but you may sometimes need to instantiate a new object. You do this by calling a class constructor. A class constructor is neither a static method nor an instance method, and therefore it does not need an instance parameter. The special method name *CONSTRUCTOR is used when prototyping a constructor.

For example, class BigDecimal has a constructor that accepts a float parameter.

This constructor would be prototyped as follows:

D bdcreate        PR              O   EXTPROC(*JAVA:
D                                      'java.math.BigDecimal':
D                                      *CONSTRUCTOR)
D    dnum                        4F   VALUE

Note that the parameter must be passed by value because it maps to the Java™ float data type.

You would call this constructor like this:

D bd              S               O   CLASS(*JAVA:
D                                           'java.math.BigDecimal')
 /free
    bd = bdcreate(5.2E9);
 /end-free

The class of the returned object is the same as the class of the constructor itself, so the CLASS keyword is redundant for a constructor, but it may be coded.



[ Top of Page | Previous Page | Next Page | Contents | Index ]