









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
Material Type: Project; Professor: Dorr; Class: OBJECT-ORIENTED PROG I; Subject: Computer Science; University: University of Maryland; Term: Spring 2007;
Typology: Study Guides, Projects, Research
1 / 17
This page cannot be seen from the preview
Don't miss anything!










CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)
z^ Start now!^ z^
CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)
CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)
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 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)
: Add a “
package
” statement to specify the package
containing the classes of this file.^ package mypackage;…public class myClass { … }
// myClass is part of mypackage
z^
This must be the
first statement
of your file. (Other than comments.)
z^ Subpackages
: Packages organized into subpackages. This is specified using the notation “
main.subpackage
”. Example:
package mypackage.mysubpackage;…public class myClass2 { … }
// myClass2 is part of mysubpackage// … which is within mypackage
z^ Packages in Eclipse
:^ File→
New→
Package
. Enter the full name of the
package (e.g. “
mypackage
” or “mypackage.mysubpackage
z^ Without Eclipse
: Just insert this yourself (“
package mypackage
CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)
z^ Classes within a package can refer to each other
without full
qualification
z^ If a class is
not^ declared
public
, it can
only be accessed by other
classes
within the package
z^ A^
public class
can be accessed from
other packages
z^ When this is done, either its name is
fully qualified
or is^
imported
z^ We can view
public classes of a package
as the “
interface
” of the
package with the outside world. (This is analogous to public methods ofa class forming its interface.)
z^ 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 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)
Files:Driver.java
Driver
Files:Circle.javaRectangle.javaOtherShape.java Files:PublicClass1.javaPublicClass2.java
Circle Rectangle OtherShape^ PublicClass1^ NonPublicClass1^ PublicClass
graphics Packages:^ graphics.shapes^ graphics.otherstuff
Files:
Classes:
CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)
File: Circle.java File: Rectangle.java File: OtherShape.java
Note: Classes of this package canbe accessed without the need forimport or using graphics.shapes.Circle
CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)
testShapes( );testOtherStuff( );} public static void testShapes( ) {Circle c = new Circle( );System.out.println( c.toString( ) );Rectangle r = new Rectangle( );} public static void testOtherStuff( ) {PublicClass1 x = new PublicClass1( );graphics.otherstuff.PublicClass1 y = new graphics.otherstuff.PublicClass1( );System.out.println( y );graphics.otherstuff.NonPublicClass1 z;} }
File: Driver.java
Note: importing Circle (but nothing else)
Okay: Circle is accessible. Compiler error: Cannot access Rectanglewithout import or qualified name.^ Compiler error: Cannot access PublicClass1without import or qualified name.^ Compiler error: Non-public classes arenever accessible outside the class.
CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)
Driver.java otherstuff/shapes/graphics/otherstuff:NonPublicClass1.classPublicClass1.java
PublicClass2.java
PublicClass1.class
PublicClass2.class
graphics/shapes:Circle.class
OtherShape.class
Rectangle.class
Circle.java
OtherShape.java
Rectangle.java
When you create newpackages in Eclipse, thisis done automatically.^ Note that nonpublic classesgenerate their own .class file,even though there is no .javafile.
CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)
z^ graphics package
: stored in directory C:\MyJavaPackages\graphics.
z^ cmsc131PictureLib.jar
: stored in C:\MyJars\cmsc131PictureLib.jar
z^ classes compiled in the
current working directory
: The current
directory is denoted by “.” (period) on most systems. z^ C:>set CLASSPATH=.;C:\MyJavaPackages;C:\MyJars\cmsc131PictureLib.jar
The list is separated by semicolons “;”
Always include “.”
jar files must belisted explicitly
CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)
: The ClassPath is already set with important default directories (e.g. the Java runtime library). Tomodify the ClassPath: z^ In the