




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This document from the cmsc 131 fall 2007 course covers java packages, their purpose, and how to define and access them. It includes examples of package hierarchies and class access within and across packages. The document also discusses package visibility and the use of .jar files and classpaths.
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)
CMSC 131 Fall 2007 Jan Plane (adapted by Bonnie Dorr)
1
CMSC 131 Fall 2007 Jan Plane (adapted by Bonnie Dorr)
2
Packages
java.awt.font: classes and interface relating to fonts. java.awt.geom: classes for defining 2-dimensional objects.
CMSC 131 Fall 2007 Jan Plane (adapted by Bonnie Dorr)
3
Access to Package Members
Accessing Package Members: Fully qualified name: E.g., javax.swing.JOptionPane Importing a single class: import javax.swing.JOptionPane; … JOptionPane.showMessageDialog( … ); Importing all the classes: import javax.swing.*; … JOptionPane.showMessageDialog( … ); Import semantics: import does not “insert” the Java files (as C/C++ do with “include” files). Instead, it tells the compiler where to look to find classes that the program refers to. Multiple import statements: You can have as many as you like. They go at the top of your .java file (before any classes or interfaces). java.lang: is automatically imported into every program.
CMSC 131 Fall 2007 Jan Plane (adapted by Bonnie Dorr)
6
Class Access and Packages
Classes within a package can refer to each other without full qualification. If a class is not given an access specifier (public or private), it is assumed to have package access and it can only be accessed by other classes within the package.
A public class can be accessed from other packages. When this is done, either its name is fully qualified or is imported. We can view public classes of a package as the “interface” of the package with the outside world. (This is analogous to public methods of a class forming its interface.)
When you import a package (e.g. import java.awt.* ) it does not import the subpackages (e.g. java.awt.font must be explicitly imported).
CMSC 131 Fall 2007 Jan Plane (adapted by Bonnie Dorr)
7
Package Visibility
Package visibility is half-way between public and private.
Package Visibility of classes:
Package visibility of class members:
CMSC 131 Fall 2007 Jan Plane (adapted by Bonnie Dorr)
8
Example
To illustrate these points, let’s consider an example of a simple hierarchical package, which we will create.
Files: Driver.java (^) Driver
Files: Circle.java Rectangle.java OtherShape.java
Files: PublicClass1.java PublicClass2.java
Circle
Rectangle
OtherShape
PublicClass NonPublicClass
PublicClass
graphics
graphics.shapes
graphics.otherstuff
Packages: Files: Classes:
CMSC 131 Fall 2007 Jan Plane (adapted by Bonnie Dorr)
9
Example: graphics.shapes
package
package graphics.shapes; public class Circle { private double radius; public String toString( ) { return "I'm a circle"; } }
package graphics.shapes; public class Rectangle { private double height, width; public String toString( ) { return "I'm a rectangle"; } }
package graphics.shapes; public class OtherShape { private Circle c; private Rectangle r; }
File: Circle.java
File: Rectangle.java
File: OtherShape.java
Note: Classes of this package can be accessed without the need for import or using graphics.shapes.Circle
CMSC 131 Fall 2007 Jan Plane (adapted by Bonnie Dorr)
12
File Structure
Java organizes the package files using your system’s directory structure.
graphics: Driver.class Driver.java otherstuff/ shapes/
graphics/otherstuff: NonPublicClass1.class PublicClass1.java PublicClass2.java PublicClass1.class PublicClass2.class
graphics/shapes: Circle.class OtherShape.class Rectangle.class Circle.java OtherShape.java Rectangle.java
When you create new packages in Eclipse, this is done automatically.
Note that nonpublic classes generate their own .class file, even though there is no .java file.
CMSC 131 Fall 2007 Jan Plane (adapted by Bonnie Dorr)
13
Packages and .jar Files
In Eclipse: In the Package Explorer window, right click on the project and select “Export →→→→ JAR file”. On Unix: jar –cvf myJarFile.jar … (list the file names and/or directories) (c = create; t = list names; x = extract; v = verbose; f = jar file)
cmsc131PictureLib.jar: A jar file we created for the picture library. cmsc131PuzzleLib.jar: A jar file we created for HW#5. C:...\Java\j2re1.4.2_05\lib\rt.jar: This is a large (20+Mbyte) file with all the class files from the Java runtime library.
CMSC 131 Fall 2007 Jan Plane (adapted by Bonnie Dorr)
14
Packages and Classpath
graphics package: stored in directory C:\MyJavaPackages\graphics. cmsc131PictureLib.jar: stored in C:\MyJars\cmsc131PictureLib.jar classes compiled in the current working directory: The current directory is denoted by “.” (period) on most systems.
C:>set CLASSPATH=.;C:\MyJavaPackages;C:\MyJars\cmsc131PictureLib.jar
Always include “.” The list is separated by semicolons “;”
jar files must be listed explicitly
CMSC 131 Fall 2007 Jan Plane (adapted by Bonnie Dorr)
15
Packages and Classpath
In Eclipse: The ClassPath is already set with important
default directories (e.g. the Java runtime library). To modify the ClassPath: