Statements |
Valid: | in a DATA step |
Category: | Action |
Type: | Executable |
Alias: | DCL |
Syntax |
DECLARE JAVAOBJ object-reference; |
DECLARE JAVAOBJ object-reference ("java-class", <argument-1 , ... argument-n>); |
specifies the object reference name for the Java object.
specifies the name of the Java class to be instantiated.
Requirement: | The Java class name must be enclosed in either double or single quotation marks. |
Requirement: | If you specify a Java package path, you must use forward slashes (/) and not periods (.) in the path. For example, an incorrect classname is "java.util.Hashtable". The correct classname is "java/util/Hashtable". |
specifies the information that is used to create an instance of the Java object. Valid values for argument depend on the Java object.
See also: | Using the DECLARE Statement to Instantiate a 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 predefined component objects from within the DATA step.
For more information, see Using DATA Step Component Objects in SAS Language Reference: Concepts.
You use the DECLARE statement to declare a Java object.
declare javaobj j;
The DECLARE statement tells SAS that the object reference J is a Java object.
After you declare the new Java object, use the _NEW_ operator to instantiate the object. For example, in the following line of code, the _NEW_ operator creates the Java object and assigns it to the object reference J:
j = _new_ javaobj("somejavaclass");
Instead of the two-step process of using the DECLARE statement and the _NEW_ operator to declare and instantiate a Java object, you can use the DECLARE statement to declare and instantiate the Java object in one step. For example, in the following line of code, the DECLARE statement declares and instantiates a Java object and assigns the Java object to the object reference J:
declare javaobj j("somejavaclass");
The preceding line of code is equivalent to using the following code:
declare javaobj j; j = _new_ javaobj("somejavaclass");
A constructor is a method that you can use to instantiate a component object and initialize the component object data. For example, in the following line of code, the DECLARE statement declares and instantiates a Java object and assigns the Java object 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("testjavaclass", 100, .8);
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.
Examples |
In the following example, a simple Java class is created. The DECLARE statement and the _NEW_ operator are used to create an instance of this class.
/* Java code */ import java.util.*; import java.lang.*; public class simpleclass { public int i; public double d; }
/* DATA step code data _null_; declare javaobj myjo; myjo = _new_ javaobj("simpleclass"); run;
In the following example, a Java class is created for a hash table. The DECLARE statement 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("mhash", 100, .8); run;
See Also |
Operator: | |||
Using DATA Step Component Objects in SAS Language Reference: Concepts |
Copyright © 2011 by SAS Institute Inc., Cary, NC, USA. All rights reserved.