DECLARE Statement, Java Object

Declares a Java object; creates an instance of and initializes data for a Java object.
Alias: DCL

Syntax

Form 1:

DECLARE JAVAOBJ object-reference;

Form 2:

DECLARE JAVAOBJ object-reference ("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.
Requirements:The Java class name must be enclosed in either double or single quotation marks.

If you specify a Java package path, you must use forward slashes (/) and not periods (.) in the path. For example, an incorrect class name is "java.util.Hashtable". The correct class name is "java/util/Hashtable".

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

The Basics

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.

Declaring a Java Object (Form 1)

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");

Using the DECLARE Statement to Instantiate a Java Object (Form 2)

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

Example 1: Declaring and Instantiating a Java Object by Using the DECLARE Statement and the _NEW_ Operator

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; 

Example 2: Using the DECLARE Statement to Create and Instantiate a Java Object

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

Using DATA Step Component Objects in SAS Language Reference: Concepts