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 creatinga file holding the definition of a class containing all thevariables and methods to be shared. They should all be declaredstatic. 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 locateComb.java and compile it. Further, loading Test.class will automaticallyload and link Comb.class.

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

 package functions;
at the very top of Comb.java, and compile it. On some systems thiscreates 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 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).

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.