Importing Java Packages and Classes

As mentioned, the classes that Sun has put together for you to use are stored in class libraries called packages. The classes we'll use to interact with XML documents in the next two chapters are also stored in packages. Although the java.lang package is already available to your code by default, the classes in other packages are not, and you must import those packages to use them. You can also import individual classes as well as whole packages. Knowing how to do this is very important in Java programming because a great deal of the resources that most programs use are in packages that you have to import.

To import a package, you use the Java import statement, which looks like this (following the Sun conventions, items in square brackets are optional, and the upright bar, , means "or", much as it does when you write DTDs):

 import [  package1  [.  package2...  ].](  classname  *); 

Note that you put a dot ( . ) between package and class names to keep them separate. The standard java packages themselves are stored in a large package called java, so the util package is really called the java.util package. (There are other large packages, including the java package, available. For example, the extensive Swing package is stored in the javax package.)

Here's an example. In this case, I want to use the Date class in the java.util package. To do that, I can import that class this way in code:

  import java.util.Date;  public class ch10_02 {     public static void main(String[] args)     {     .     .     .     } } 

Now I'm able to use the Date class in code. To do that, I create a new Date object with the new operatorwhich is how you create objects from classes in JavaScript as well. (In fact, we used the JavaScript Date class and created a new object of that class in Chapter 6.) The new Date object represents today's date, which I can display like this. Note that I'm also adding a pair of empty parentheses after the Date class to indicate that I'm not passing any value to that class's constructor (as we saw in Chapter 6, a class's constructor is a method that runs when an object is created from the class, allowing you to initialize the object):

Listing ch10_02.java
 import java.util.Date; public class ch10_02 {     public static void main(String[] args)     {  System.out.println("Today's date is " + new Date());  } } 

When you compile and run this application, this is the kind of result you'll see:

 %java ch10_02  Today's date is Mon May 22 16:28:53 EDT 2003 

In this case, I imported a specific class, the Date class, from the java.util package. However, you can import all classes from a package at once with the * wildcard like this, where I'm importing all java.util classes (note that this does not make the ch10_02.class file any largeronly those classes that are actually referenced in the code are used when building the final bytecode filethe bytecode file ch10_02.class will be the same size if you use either the statement import java.util.Date; or import java.util.*; .):

  import java.util.*;  public class ch10_02 {     public static void main(String[] args)     {         System.out.println("Today's date is " + new Date());     } } 

You can also import classes that you've created. For example, say that you've created a class named Display that uses a method named showImage to display an image on the user 's screen. You might create a new object of the Display class and use the showImage method something like this:

 public class ch10_02  {     public static void main(String[] args)     {  (new Display()).showImage("flowers.gif");  } } 

When you've created the file Display.class, you can import the Display class into your program like this:

  import Display;  public class ch10_02 {     public static void main(String[] args)     {         (new Display()).showImage("flowers.gif");     } } 

Storing Display.class

This technique relies on having Display.class in the same directory as the application you're compiling so that the import statement can find it. On the other hand, you might want to store Display.class in another directory, such as c:\display. In that case, you have to add c:\display to the Java environment variable CLASSPATH . We'll see more about this in the next chapter, or you can see the Java documentation for all the details.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net