Browse DevX
Sign up for e-mail newsletters from DevX

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



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Creating Java packages or library files

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?

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);
         outField = new TextField("RESULT", 20);

      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

in Test.java with:
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.



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