Previous Page | Next Page

SAS Component Language Dictionary

_NEW_



Creates an object and runs an associated class constructor
Category: Object Oriented

Syntax
Examples
Example 1: Creating an Instance of an Object and Running a Class Constructor
Example 2: Using a Class That Has an Overloaded Constructor
See Also

Syntax

object-id=_NEW_ class-name(<arg1, arg2, . . . , argn>);

object-id

contains the identifier for the new object.

Type: Numeric, Classes, and Object

class-name

is the name of the class from which to create the object. This can be a one- to four-level name. If class-name is a one- or two-level name, and if the CLASS entry that defines class-name is not in the application catalog, then class-name must exist in one of the catalogs defined by the IMPORT statement. Otherwise, the compiler produces an error message.

arg1, arg2, . . . , argn

are the arguments that are passed to the class constructor.

Type: Character


Examples


Example 1: Creating an Instance of an Object and Running a Class Constructor

The _NEW_ operator enables you to create an instance of an object and run a class constructor all in one step.

X.SCL
class Complex;
  private num x y;

  Complex: method a1: num a2: num;
           x = c.x;
           y = c.y;
  endmethod;
endclass;

Y.SCL
init:
  dcl Complex c = _new_ Complex(1,2);
  dcl Complex c2 = _new_ Complex(c);
  return;
 


Example 2: Using a Class That Has an Overloaded Constructor

Where you have a class, Complex, that has an overloaded constructor, one version takes two numeric arguments, and the other takes a complex number.

In the first _NEW_ statement, you create a complex number, 1 + 2i, by passing in the real and imaginary parts (1 and 2) as arguments to the _NEW_ operator. _NEW_ first creates an instance of the Complex class, and then calls the appropriate constructor based on its argument list. In this case, it calls the constructor that takes two numeric arguments.

In the second _NEW_ statement, you create a complex number by passing in another complex number (in this case, the first one you created, 1 + 2i). _NEW_ calls the second constructor in the Complex class -- the one that takes a complex number as an argument.

Constructors must always have the same name as the class in which they are located, and they must always be void methods (that is, they do not return a value). Constructors provide a convenient way to initialize a class before you begin using it.

For example, if you didn't use constructors in the above example, you would have to create another set of methods to initialize the complex number and call those separately, as in the following example:

X.SCL
class Complex;
  private num x y;

  set: method a1: num a2: num;
           x = a1;
           y = a2;
  endmethod;

  set: method c: Complex;
           x = c.x;
           y = c.y;
  endmethod;
endclass;

Y.SCL
init:
 dcl Complex c = _new_ Complex();
 dcl Complex c2 = _new_ Complex();
 c.set(1,2);
 c2.set(c);
 return;

You can overload constructors as shown above, or you can have only one constructor, or you can have none. If you don't supply a constructor in a class, the _NEW_ operator still attempts to run a parent class constructor. In the following example, the _NEW_ operator for the class Y calls the constructor in X, since X is the parent of Y.

X.SCL
class x;
 x: method n: num;
    put n=;
 endmethod;
endclass;

Y.SCL
class y extends x;
endclass;

Y.SCL
init:
 dcl y y = _new_ y(100);
 return;

This behavior applies to all classes that are instantiated by using _NEW_ -- even those that have no explicit constructors.

If you do not supply a constructor for your classes, you can still use _NEW_ with no arguments because OBJECT.CLASS (which all classes extend) contains a constructor that takes no arguments and performs no actions. This "dummy" constructor needs to be overridden if you want to supply a constructor that does not take an argument by using the following:

class x;
 x: method /(state='0');
    put 'in x constructor';
 endmethod;
endclass;

Constructors can be used only in conjunction with the _NEW_ operator. They are not called if the object is instantiated in some other way, such as by using the _NEW_ method in CALL SEND, or by dropping the object in a frame. In particular, in the case of a visual object, you cannot run a constructor after the object is instantiated, but only before it is displayed. Therefore, you cannot use a constructor to initialize the object. For these types of objects, you must either use the _NEO_ operator or use the _NEW_ method with CALL SEND.


See Also

DECLARE

IMPORT

LOADCLASS

_NEO_

Previous Page | Next Page | Top of Page