|
||||||
When using any object oriented language like Java the programmer will create classes - lots of them. A programmer can organize these classes by introducing Java packages.
Java is an object oriented programming language and any Java programmer will quickly build a large number of different classes for use in each application that they develop. Initially it may be tempting to store the classes in the same directory as the end application, but as the complexity applications grow so will the number of classes. The answer is to organize the classes into packages. The Java PackageA Java package is simply a directory that contains one or more class files. It is, therefore, just a matter of:
Of course, nothing is ever quite that simple. Creating a Java PackageThe first step is (rather obviously) to create the directory for the package. This can go in one of two places:
So, for example, if CLASSPATH contains C:\Java\Packages then the new package can be created there, for example C:\Java\Packages\suite101, and once the directory has been created then classes can be added to it. The first line of a class in a package is essential - it must contain the name of of the package (i.e. the directory in which the class resides): package suite101;
No code is allowed above the package statement, but after that critical line the class can be defined as normal. For example it can call any packages that it requires: import java.util.* ;
import java.text.* ;
In this example the class contains a single method which returns today's date formatted for the current locale: public class formatted_date {
public static String today () {
Date mydate = new Date();
String dateString = DateFormat.getDateInstance().format(mydate);
return dateString;
}
}
As always the code must be saved to a text file with the same name as the class - in this case that's C:\Java\Packages\suite101\formatted_date.java. Using a Java PackagePackages are loaded into new applications by using the import method as shown in the above example where the following line was used: import java.util.* ;
This is a statement recognized by many Java programmers and means "load all of the the classes stored in the the java.util package". Custom packages are loaded into a Java application exactly the same way: import suite101.* ;
This will load any classes found in the suite101 directory (the location of which is defined by the CLASSPATH environmental variable). The formatted_date class can now be used in the class for the new application: public class use_packages {
public static void main(String args[]) {
formatted_date newday = new formatted_date();
System.out.println(newday.today());
}
}
The application code must be saved to a file with the same name as its class, but this file may be placed in any location on the computer (for example C:\Java\Apps\creating_packages\use_packages.java) and this can be compiled and run as required. SummaryJava packages are used to group and organize classes:
Giving the Java programmer the ability to create and to organize classes in the most efficient ways possible.
The copyright of the article How to Create a Java Package in Javascript/Java Programming is owned by Mark Alexander Bain. Permission to republish How to Create a Java Package in print or online must be granted by the author in writing.
Comments
Sep 3, 2009 7:20 AM
Guest :
Oct 8, 2009 2:10 PM
Guest :
Oct 12, 2009 12:10 PM
Guest :
Oct 27, 2009 5:48 AM
Guest :
4 Comments
|
||||||
|
|
||||||
|
|
||||||