Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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;

3 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.

4 HOW TO ADD A CLASS TO A PACKAGE Before code defining class: package edu.mwsu; In code using package: import edu.mwsu.*;

5 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

6 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

7 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

8 DOCUMENTATION Java SDK took javadoc generates HTML documentation form source files. Add comments starting with /** … */ Can use HTML modifiers like … tags like @param Put comment before the class or method definition.

9 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


Download ppt "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."

Similar presentations


Ads by Google