Creating Java packages or library files

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 creatinga file holding the definition of a class containing all thevariables and methods to be shared. They should all be declaredstatic. For example:

 //   // 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):
 //   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 ( == 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 is compiled, the Java compiler will automatically and compile it. Further, loading Test.class will automaticallyload and link Comb.class.

If you plan using the methods and variables in in futureapplications and applets, place the line:

 package functions;
at the very top of, and compile it. On some systems thiscreates a directory called functions, and places Comb.class inside.Otherwise, you must do this yourself.
See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide

Next, either replace the call

in with:
or, if this is a hassle, simply place the line
import functions.*;
at the top of

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 thecurrent directory for a directory called functions. If this directory does not exist, it next looks in each directory listed in CLASSPATH.

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


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist