If you want to create an instance of an object type, you must declare a variable of the object type and then initialize the declared object variable.

The syntax for declaring an object variable is as follows:

object obj_type

where, object is the identifier assigned to the object variable, and obj_type is the identifier of the previously defined object type.

After you declare an object variable, you must invoke a constructor method to initialize the object with values. Use the following syntax to invoke the constructor method:

[NEW] obj_type ({expr1 | NULL} [, {expr2 | NULL} ] [, ...])

where, obj_type is the identifier of the object type's constructor method, and the constructor method has the same name as the previously declared object type.

expr1, expr2, ... are expressions that are type-compatible with the first attribute of the object type, the second attribute of the object type, and so on. If an attribute is of an object type, the corresponding expression can be NULL, an object initialization expression, or any expression that returns the object type.

The following anonymous block declares and initializes a variable:

DECLARE
    v_emp           EMP_OBJ_TYPE;
BEGIN
    v_emp := emp_obj_type (9001,'JONES',
        addr_obj_type('123 MAIN STREET','EDISON','NJ',08817));
END;

The variable v_emp is declared with a previously defined object type named EMP_OBJ_TYPE. The body of the block initializes the variable by using the emp_obj_type and addr_obj_type constructors.

You can include the NEW keyword when you create an instance of an object in the body of a block. The NEW keyword invokes the object constructor whose signature matches the parameters provided.

The following example declares two variables named mgr and emp. Both the variables are of EMP_OBJ_TYPE. mgr is initialized in the declaration, while emp is initialized to NULL in the declaration and is assigned a value in the body.

DECLARE
    mgr  EMP_OBJ_TYPE := (9002,'SMITH',NULL);
    emp  EMP_OBJ_TYPE;
BEGIN
    emp := NEW EMP_OBJ_TYPE (9003,'RAY',NULL);
END;

In PolarDB for PostgreSQL(Compatible with Oracle), you can use the following alternate syntax in place of the constructor method:

[ ROW ] ({ expr1 | NULL } [, { expr2 | NULL } ] [, ...])

ROW is an optional keyword if you specify two or more expressions within the parenthesis-enclosed, comma-delimited list. If you only specify one expression, you must specify the ROW keyword.