Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java Language
Expertise: Beginner
May 27, 1997

Creating Java packages or library files

Question:
I am creating an application by using Symantec Cafe wherein I am using one applet, from which I am calling a series of frames. I have some common functions and variables amongst these frames that I must write in each frame code. I am thinking of making a Java package or library file that I would like to import when required in my Frame code. Is it possible?

Answer:
There are two ways for applets to share code. Begin by creating a file holding the definition of a class containing all the variables and methods to be shared. They should all be declared static. For example:

 // Comb.java

   // useful combinatorial functions

   class Comb {  

      // = 1 * 2 * ... * n
      public static int fact(int n) {
         int result = 1;
         for(int i = 1; i < n; i++)
            result = i * result;
         return result;
      }

      // etc.

   }
Each applet can simply access the fact method by calling Comb.fact(n):
 // Test.java

   public class Test extends Applet {

      private TextField inField, outField;
   
      public void init() {
         inField = new TextField("INPUT", 20);
         add(inField);
         outField = new TextField("RESULT", 20);
         add(outField);
      }

      public boolean action (Event e, Object arg) {
         if (e.target == inField) {
            int n = Integer.valueOf(inField.getText()).intValue();
            int m = Comb.fact(n);
            outField.setText("" + m);
         }
         else return false;  // event not handled
         return true; // event handled
      }

   } 
When Test.java is compiled, the Java compiler will automatically locate Comb.java and compile it. Further, loading Test.class will automatically load and link Comb.class.

If you plan using the methods and variables in Comb.java in future applications and applets, place the line:

 package functions;
at the very top of Comb.java, and compile it. On some systems this creates a directory called functions, and places Comb.class inside. Otherwise, you must do this yourself.

Next, either replace the call

 Comb.fact(n)
in Test.java with:
 functions.Comb.fact(n)
or, if this is a hassle, simply place the line
import functions.*;
at the top of Test.java.

Of course the functions package can contain many classes, other packages, even .zip files made from classes.

When the Java compiler sees "import functions.*;" it looks in the current directory for a directory called functions. If this directory does not exist, it next looks in each directory listed in CLASSPATH.

Thus, if you really plan using the functions package often, you can start a special directory containing functions and other custom packages, then add the directory's name to CLASSPATH (in autoexec.bat).

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date