Methods all follow the same syntax rules (see the description of the METHOD instruction, elsewhere), and are usually invoked in context of an existing object. A special kind of method, called a constructor method, is used to actually create an object.
Constructor methods always have the same name as the class in which they are found, and construct an object of that class. There will always be at least one constructor if objects are to be created (NetRexx will add a default constructor that takes no arguments if none are provided). All constructors follow the following rules:
As a convenience, NetRexx will add a default call to super() if the first instruction in a constructor is not a call to this() or super().
Here is an example of a class with two constructors, showing the use of this() and super(), and taking advantage of the assumptions:
class MyCharsproperties private value=char[] -- the data 'in' the object
method MyChars(c=char[]) -- construct the object from a char array super() -- initialize superclass (in this case Object) value=c -- record the value (without copying)
method MyChars(s=String) -- construct the object from a String this(s.toCharArray()) -- convert to char[] and use the above
Objects of type 'MyChars' could then be created thus:
myvar=MyChars("From a string")
or by using an argument that has type char[]. Note that all references to constructors must be identified by the use of parentheses, to distinguish them from references to the type (class) of the same name.
[ previous section | contents | next section ]
From 'netrexx.doc', version 0.75.
Copyright(c) IBM Corporation, 1996. All rights reserved. ©