René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback
 

Constructors [PL/SQL]

Constructors are used to initialize an object. In most case, this initialization assigns values to the object's members.
A constructor is a function whose name is equal to its object's name. The function keyword is prepended with the constructor keyword.
While an ordinary function returns some type, a constructor function returns self as result.
The following example declares an object with one member (txt) and a constructor that takes two arguments:
create type ctor_test as object (
  txt varchar2(100),

  constructor function ctor_test(pat varchar2, repeat number) return self as result,

  member procedure print
);
/
Here's the body of the object. The constructor initializes the object's member (txt);
create type body ctor_test as

  constructor function ctor_test(pat varchar2, repeat number) return self as result is begin
    for iii in 1 .. repeat loop
      txt := txt || pat;
    end loop;
    return;
  end;

  member procedure print is begin
    dbms_output.put_line(txt);
  end print;

end;
/

Let's see it in action:
declare
  v_ctor_test_1 ctor_test;
  v_ctor_test_2 ctor_test;
begin

  v_ctor_test_1 :=     ctor_test('bla', 5);
  v_ctor_test_2 := new ctor_test('foo', 5);

  v_ctor_test_1.print;
  v_ctor_test_2.print;

end;
/
Here's the output:
blablablablabla
foofoofoofoofoo

Collection types

See also this link.