13 - 09/27/10 23:08:22 CS 61B: Lecture 13 Monday, September...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
09/27/10 23:08:22 1 13 CS 61B: Lecture 13 Monday, September 27, 2010 Today’s reading: JAVA PACKAGES ============= In Java, a _package_ is a collection of classes and Java interfaces, and possibly subpackages, that trust each other. Packages have three benefits. (1) Packages can contain hidden classes that are used by the package but are not visible or accessible outside the package. (2) Classes in packages can have fields and methods that are visible by all classes inside the package, but not outside. (3) Different packages can have classes with the same name. For example, java.awt.Frame and photo.Frame. Here are two examples of packages. (1) java.io is a package of I/O-related classes in the standard Java libraries. (2) Homework 4 uses "list", a package containing the classes DList and DListNode. You will be adding two additional classes to the list package. Package names are hierarchical. java.awt.image.Model refers to the class Model inside the package image inside the package awt inside the package java. Using Packages -------------- You can address any class, field, or method with a fully-qualified name. Here’s an example of all three in one. java.lang.System.out.println("My fingers are tired."); Java’s "import" command saves us from the tedium of using fully-qualified names all the time. import java.io.File; // Can now refer to File class, not just java.io.File. import java.io.*; // Can now refer to everything in java.io. Every Java program implicitly imports java.lang.*, so you don’t have to import it explicitly to use System.out.println(). However, if you import packages that contain multiple classes with the same name, you’ll need to qualify their names explicitly throughout your code. java.awt.Frame.add(photo.Frame.canvas); Any package you create must appear in a directory of the same name. For example, the photo.Frame class bytecode appears in photo/Frame.class, and x.y.z.Class appears in x/y/z/Class.class. Where are the photo and x directories? They can appear in any of the directories on your CLASSPATH
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/10/2012 for the course CS 61B taught by Professor Canny during the Fall '01 term at University of California, Berkeley.

Page1 / 2

13 - 09/27/10 23:08:22 CS 61B: Lecture 13 Monday, September...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online