Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 13: Speed <em>and</em> portability

How to use native methods without restricting class usability

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 2

    public void convolve() {
      if (_native)
        nativeConvolve();
      else
        fallbackConvolve();
    }
    private native void nativeConvolve();
    private void fallbackConvolve() {
      ...
    }


In this example, a convolution operation can be performed either by native method nativeConvolve or by fallback method fallbackConvolve. The distinction between these two methods is irrelevant to the class user, who simply invokes convolve to perform the operation. If the dynamic library has been installed, the user gets the benefits of faster execution; if the dynamic library is unavailable, the operation can still be carried out, albeit more slowly.

That's all there is to it! Once you've written and compiled your class, you add the native method support in the normal way. (See the current version of Sun's Java Tutorial for an explanation of how to do this.)

About the author

Nick Efford is a lecturer at the School of Computer Studies at the University of Leeds, U.K. Nick's research has been concerned mainly with the knowledge-based segmentation of medical images and the identification of objects of interest, using 2-D and 3-D models of object shape. Nick currently teaches image processing at the School of Computer Studies.
  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources