_NEW_ Operator, Java Object

Creates an instance of a Java object.

Valid in: DATA step
Applies to: Java object

Syntax

object-reference = _NEW_ JAVAOBJ ("java-class", <argument-1, ...argument-n> );

Arguments

object-reference

specifies the object reference name for the Java object.

java-class

specifies the name of the Java class to be instantiated.

Requirement The Java class name must be enclosed in either single or double quotation marks.

argument

specifies the information that is used to create an instance of the Java object. Valid values for argument depend on the Java object.

Details

To use a DATA step component object in your SAS program, you must declare and create (instantiate) the object. The DATA step component interface provides a mechanism for accessing the predefined component objects from within the DATA step.
If you use the _NEW_ operator to instantiate the Java object, you must first use the DECLARE statement to declare the Java object. For example, in the following lines of code, the DECLARE statement tells SAS that the object reference J is a Java object. The _NEW_ operator creates the Java object and assigns it to the object reference J.
declare javaobj j;
j = _new_ javaobj("somejavaclass" );
Note: You can use the DECLARE statement to declare and instantiate a Java object in one step.
A constructor is a method that is used to instantiate a component object and to initialize the component object data. For example, in the following lines of code, the _NEW_ operator instantiates a Java object and assigns it to the object reference J. Note that the only required argument for a Java object constructor is the name of the Java class to be instantiated. All other arguments are constructor arguments for the Java class itself. In the following example, the Java class name, testjavaclass, is the constructor, and the values 100 and .8 are constructor arguments.
declare javaobj j;
j = _new_ javaobj("testjavaclass", 100, .8);
For more information about the predefined DATA step component objects and constructors, see Using DATA Step Component Objects in SAS Language Reference: Concepts.

Comparisons

You can use the DECLARE statement and the _NEW_ operator, or the DECLARE statement alone to declare and instantiate an instance of a Java object.

Example: Using the _NEW_ Operator to Instantiate and Initialize a Java Class

In the following example, a Java class is created for a hash table. The _NEW_ operator is used to create and instantiate an instance of this class by specifying the capacity and load factor. In this example, a wrapper class, mhash, is necessary because the DATA step's only numeric type is equivalent to the Java type DOUBLE.
/* Java code */
import java.util.*;
public class mhash extends Hashtable;
{
   mhash (double size, double load)
      {
         super ((int)size, (float)load);
      }
}
/* DATA step code */
data _null_;
   declare javaobj h;
   h = _new_ javaobj("mhash", 100, .8);
run;

See Also

Using DATA Step Component Objects in SAS Language Reference: Concepts