Java Packages - allow you to group classes as a collection Standard Java library has packages: java.lang and java.util … Also hierarchical: java and javax packages WHY? Guarantee uniqueness of class names Sun recommends your highest package be domain name in reverse edu.mwsu Can create subpackages within it NOTE: no logical relationship between packages…just physical
USING PACKAGES Can use all classes within package and all public classes in other classes as long as you 1. Qualify java.util.Date today = new java.util.Date ( ); 2. import package import java.util.*; 3. import class import java.util.Date;
careful with name conflicts EXAMPLE import java.util.*; import java.sql.*; //both have Date class SOLNs: import java.util.Date; to use this one use qualifiers if you need both.
HOW TO ADD A CLASS TO A PACKAGE Before code defining class: package edu.mwsu; In code using package: import edu.mwsu.*;
Directory structure Put source file of package in subdirectory edu\mwsu of program directory. (program directory) ManipCN.java edu/ mwsu/ ComplexNumber.java Compile in program directory: javac ManipCN.java
TO SHARE PACKAGES Need to have special directory. I called mine C:\javasrc\classes NOTE: javasrc is my BASE directory I put files ComplexNumber.java in C:\javasrc\classes\edu\mwsu I put ManipCN.java in C:\javasrc
NOW TO COMPILE javac –classpath C:\javasrc\classes;. ; C:\archives\archives.jar ManipCN.java OR In Windows, set CLASSPATH env’t variable to C:\javasrc\classes;. ; C:\archives\archives.jar
DOCUMENTATION Java SDK took javadoc generates HTML documentation form source files. Add comments starting with /** … */ Can use HTML modifiers like … tags Put comment before the class or method definition.
USING JAVADOC 1. change to directory that contains source files to document 2. javadoc –d docDir package1 package2 OR javadoc –d docDire *.java for files in default package if you omit docDir, puts in current directory