16PackagesSmall - Lecture Set # 16: Packages 1. Packages...

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

View Full Document Right Arrow Icon
1 CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dorr) Lecture Set # 16: Packages 1. Packages CMSC 131 Fal 2009 Jan Plane (adapted by Bonnie Dorr) 1 Java Program Organization c Program Organization : c Java program : is composed of 1 or more Java source files . c Source file : can have 1 or more class and/or interface declarations . (In our projects we have implemented one class/interface per file.) c Public Class/Interface : If a class/interface is declared public the source file must use the same name . c Only one public class/interface is allowed per source file . (Can you have non-public classes? We will discuss this later.) c Packages : When a program is very large, its classes can be further organized hierarchically into packages. CMSC 131 Fal 2009 Jan Plane (adapted by Bonnie Dorr) 2 Packages c Package : a collection of related classes and/or interfaces. c Examples : The Java API javax.swing : classes dealing with the development of GUIs. java.lang : essential classes required by the Java language. java.text : facilities for formatting text output. java.util : classes for storing/accessing collections of objects. java.net : for network communication. c Hierarchical : Packages can be divided into subpackages. java.awt : classes for basic GUI elements and graphics. java.awt.font : classes and interface relating to fonts. java.awt.geom : classes for defining 2-dimensional objects. There is no limit to the nesting depth.
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 CMSC 131 Fal 2009 Jan Plane (adapted by Bonnie Dorr) 3 Access to Package Members c Review of Package Basics : 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 Fal 2009 Jan Plane (adapted by Bonnie Dorr) 4 Defining your own package c Why packages? Packages enable a programmer organize the code into smaller logically related units. A large program may consists of hundreds of classes. (Although we may not need to use them for the little projects in CMSC 131, but it is important to know how to create packages for when you will need them.) c Every class is part of some package : c Default package : If you do not specify a package a class becomes part of the " default package
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/04/2011 for the course CMSC 131 taught by Professor Plane during the Fall '08 term at Maryland.

Page1 / 6

16PackagesSmall - Lecture Set # 16: Packages 1. Packages...

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

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