Skip to Main Content Skip to Search
Product Documentation

Creating an Instance of the Class

What Is an Instance?

With a MATLAB Java class, it is necessary first to create an instance of the class, since the methods are non-static.

Suppose you build a component named MyComponent with a class named MyClass. Here is an example of creating an instance of the MyClass class:

MyClass instance = new MyClass();

Code Fragment: Instantiating a Java Class

The following Java code shows how to create an instance of a class that was built with MATLAB Builder JA. The application uses a Java class that encapsulates a MATLAB function, myprimes.

/*
 * useMyClass.java uses myClass
 */

/* Import all com.mathworks.toolbox.javabuilder classes */
import com.mathworks.toolbox.javabuilder.*;

/* Import all com.mycompany.mycomponent classes */
import com.mycompany.mycomponent.*;

/*
 * useMyClass 
 */
public class useMyClass
{
   /** Constructs a new useMyClass */
   public useMyClass()
   {
      super();
   }

   /* Returns an array containing the primes between 0 and n */
   public double[] getPrimes(int n) throws MWException
   {
      myClass Class = null;
      Object[] y = null;

      try
      {
         Class = new myClass ();
         y = Class.myPrimes(1, new Double((double)n));
          /* The above signature returns outputs in an 
              object array. You must know the output
              type to know what type to cast.  */   
          return (double[])((MWArray)y[0]).getData();
      }

      catch (MWException e) { 
          // something went wrong while 
          //    initializing the component - the 
          //    MWException's message contains more information 
      } 

      finally
      {
         MWArray.disposeArray(y);
         if (Class != null)
         		Class.dispose();
      }
   }
}

The import statements import packages that define all the classes the program requires. These classes are defined in javabuilder.* and mycomponent.*; the latter defines the class myClass.

The following statement instantiates the class myclass:

Class = new myClass();

The following statement calls the class method myprimes:

y = Class.myPrimes(1, new Double((double)n));

The sample code passes a java.lang.Double to the myPrimes method. The java.lang.Double is automatically converted to the double data type required by the encapsulated MATLAB myPrimes function.

When myPrimes executes, it finds all prime numbers between 0 and the input value and returns them in a MATLAB double array. This array is returned to the Java program as an MWNumericArray with its MWClassID property set to MWClassID.DOUBLE.

The myPrimes method encapsulates the myPrimes function.

myPrimes Function

The code for myPrimes is as follows:

function p = myPrimes(n)
%   MYPRIMES Returns the primes between 0 and n.
%   P = MYPRIMES(N) Returns the primes between 0 and n.
%   This file is used as an example for the MATLAB 
%   Builder for Java product.

%   Copyright 2001-2010 The MathWorks, Inc.

if length(n) ~= 1
   error('N must be a scalar');
end

if n < 2
   p = zeros(1,0);
   return
end

p = 1:2:n;
q = length(p);
p(1) = 2;

for k = 3:2:sqrt(n)
   if p((k+1)/2)
      p(((k*k+1)/2):k:q) = 0;
   end
end

p = (p(p>0));
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS