Declaring and Initializing Objects
END plus;
MEMBER FUNCTION less (x Rational) RETURN Rational IS
-- return difference of SELF - x
r Rational;
BEGIN
r := Rational(num * x.den - x.num * den, den * x.den);
r.normalize;
RETURN r;
END less;
MEMBER FUNCTION times (x Rational) RETURN Rational IS
-- return product of SELF * x
r Rational;
BEGIN
r := Rational(num * x.num, den * x.den);
r.normalize;
RETURN r;
END times;
MEMBER FUNCTION divby (x Rational) RETURN Rational IS
-- return quotient of SELF / x
r Rational;
BEGIN
r := Rational(num * x.den, den * x.num);
r.normalize;
RETURN r;
END divby;
END;
Declaring and Initializing Objects
Once an object type is defined and installed in the schema, you can use it to declare objects in any PL/SQL block, subprogram, or package. For example, you can use the object type to specify the datatype of an attribute, column, variable, bind variable, record field, table element, formal parameter, or function result. At run time, instances of the object type are created; that is, objects of that type are instantiated. Each object can hold different values.
Such objects follow the usual scope and instantiation rules. In a block or subprogram, local objects are instantiated when you enter the block or subprogram and cease to exist when you exit. In a package, objects are instantiated when you first reference the package and cease to exist when you end the database session.
10-24
PL/SQL User’s Guide and Reference
Declaring and Initializing Objects
Declaring Objects
You can use object types wherever built-in types such as
CHAR
or
NUMBER
can be used. In the block below, you declare object r
of type
Rational
. Then, you call the constructor for object type
Rational
to initialize the object. The call assigns the values 6 and 8 to attributes num
and den
, respectively.
DECLARE
r Rational;
BEGIN
r := Rational(6, 8);
dbms_output.put_line(r.num); -- prints 6
You can declare objects as the formal parameters of functions and procedures. That way, you can pass objects to stored subprograms and from one subprogram to another. In the next example, you use object type
Account
to specify the datatype of a formal parameter:
DECLARE
...
PROCEDURE open_acct (new_acct IN OUT Account) IS ...
In the following example, you use object type
Account to specify the return type of a function:
DECLARE
...
FUNCTION get_acct (acct_id IN INTEGER) RETURN Account IS ...
Initializing Objects
Until you initialize an object by calling the constructor for its object type, the object is atomically null. That is, the object itself is null, not just its attributes. Consider the following example:
DECLARE
r Rational; -- r becomes atomically null
BEGIN
r := Rational(2,3); -- r becomes 2/3
A null object is never equal to another object. In fact, comparing a null object with any other object always yields
NULL
. Also, if you assign an atomically null object to another object, the other object becomes atomically null (and must be reinitialized).
Likewise, if you assign the non-value
NULL
to an object, the object becomes atomically null, as the following example shows:
PL/SQL Object Types
10-25
Declaring and Initializing Objects
DECLARE
r Rational;
BEGIN
r Rational := Rational(1,2); -- r becomes 1/2
r := NULL; -- r becomes atomically null
IF r IS NULL THEN ... -- condition yields TRUE
A good programming practice is to initialize an object in its declaration, as shown in the following example:
DECLARE
r Rational := Rational(2,3); -- r becomes 2/3
How PL/SQL Treats Uninitialized Objects
In an expression, attributes of an uninitialized object evaluate to
NULL
. Trying to assign values to attributes of an uninitialized object raises the predefined exception
ACCESS_INTO_NULL
. When applied to an uninitialized object or its attributes, the
IS NULL
comparison operator yields
TRUE
.
The following example illustrates the difference between null objects and objects with null attributes:
DECLARE
r Rational; -- r is atomically null
BEGIN
IF r IS NULL THEN ... -- yields TRUE
IF r.num IS NULL THEN ... -- yields TRUE
r := Rational(NULL, NULL); -- initializes r
r.num := 4; -- succeeds because r is no longer atomically null
-- even though all its attributes are null
r := NULL; -- r becomes atomically null again
r.num := 4; -- raises ACCESS_INTO_NULL
EXCEPTION
WHEN ACCESS_INTO_NULL THEN
...
END;
Calls to methods of an uninitialized object raise the predefined exception
NULL_SELF_DISPATCH
. When passed as arguments to
IN parameters, attributes of an uninitialized object evaluate to
NULL
. When passed as arguments to
OUT
or
IN
OUT
parameters, they raise an exception if you try to write to them.
10-26
PL/SQL User’s Guide and Reference