Creating Java packages or library files

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing