NEW and DYNAMIC-NEW statements
- Last Updated: March 19, 2024
- 4 minute read
- OpenEdge
- Version 12.8
- Documentation
NEW and DYNAMIC-NEW statements
Syntax
This
is the syntax for instantiating a class using either the NEW statement
or the DYNAMIC-NEW statement:
|
Element descriptions for this syntax diagram follow:
- object-reference
- The name of a data element (for example, variable, writable property, or parameter) appropriately defined as a class or interface type.
- NEW class-type-name
- Where class-type-name is
the type name of the class to instantiate; it cannot be an interface
type name. This can be the fully qualified type name or the unqualified
class name, depending on the presence of an appropriate
USINGstatement in the class or procedure file. For more information on forming type names for class-based objects, see Define and reference object type names. For more information on theUSINGstatement, see Reference an object type name without its package. - DYNAMIC-NEW expression
- Where expression is
a character expression that evaluates, at run time, to the fully-qualified
type name of the class to instantiate; it cannot be an interface
type name. For more information on forming type names for class-based
objects, see Define and reference object type names.
The value of expression is
not affected by any
USINGstatements becauseUSINGstatements only operate with a compile-time type name, as you provide for theNEWstatement. TheDYNAMIC-NEWstatement is useful for OERA-compliant applications, where you want to instantiate a class whose type (specified in expression) is passed as a parameter to a method. Such a method allows you to instantiate one of a set of classes without the need to perform anIForCASEstatement test for the identity of the class type. This works especially well when the possible class types specified by expression all implement the same set of interfaces. - [ parameter [ , parameter]...]
- The parameters, if any, passed to a
PUBLIC,PACKAGE-PROTECTED,PACKAGE-PRIVATE, orPROTECTEDconstructor defined by the class-type-name or expression class. If the class defines more than onePUBLIC,PACKAGE-PROTECTED,PACKAGE-PRIVATE, orPROTECTEDconstructor, the constructors are overloaded and these parameters identify the constructor to use for instantiating the class. ForDYNAMIC-NEW, the constructors for all possible class types specified by expression must all take the same number of parameters defined with compatible data types and modes. For any one class instantiated byDYNAMIC-NEW, its instance constructors can be overloaded by the number of parameters, by the parameter data types or by the modes.
Whenever you create
an instance of a class, the specified constructor of the instantiated class, as well as the
specified constructors of any super classes in its class hierarchy, are run. The
instantiated object also gets its own copy of PUBLIC,
PACKAGE-PROTECTED, PACKAGE-PRIVATE, and PROTECTED data members and
properties defined by all classes in its class hierarchy. That is, all instances of a class
share a single set of methods defined for that class, but each instance has its own data not
shared with any of the others. Thus, just as with persistent procedures, each instance of a
class is a separate entity with its own instance data. For more information on how ABL
constructs a class-based object, see Construct an object.
This example, from the Main class
described previously creates instances of two sample classes in its constructor:
|
You can also use the NEW function
in any expression outside of a NEW statement, without
assigning the object reference, as long as the expression is appropriate
for referencing the instantiated class. An example is when accessing
a property on the object reference returned by the NEW function
that returns a value that is data-type compatible with the expression.
For
example, if you have a class that takes a default constructor, Deposit,
and this class defines a DECIMAL property, Maximum,
you can both instantiate the class and display the property value
directly on the object reference returned by the NEW function
in the MESSAGE statement that follows:
|
This Maximum property reference
represents a chained reference on the result of the NEW function.
Note that in order to reference a member of a class by chaining
on the instantiating NEW function, you must bracket
the function reference with parentheses and follow the closing parenthesis
with the member reference. Note also that when you do not assign
the object reference from a class instantiation, as in this example,
ABL automatically deletes the instantiated class at some point after
it is no longer needed in the expression.