Lecture 07 - Packages
Lecture 07 - Packages
Data Structures
Lecture 07 – Packages
Packages
3
● By the end of this, lecture you must be able to:
• Understand what packages are
• Understand various Java’s built in packages
• Understand and differentiate the 2
categories of packages
• Create a user-defined package
• Create a small Java program using a user-
defined packages
4
Introduction
Package
Used to group related classes into a folder
● What are they used for?
• To write a better and maintainable code
• To prevent possible name conflicts.
● Two categories:
• Built-in Packages (See Java API)
• Come bundled with the language
• User-defined Packages
• We have to create them 5
a) Built-in packages
● Java API is a huge library of pre-written classes
• Rem: class implement behavior of objects
belonging to it
• Examples of classes:
• Scanner: input (e.g. keyboard)
• PrintWriter: file output
• StringTokenizer: breaking a String into
tokens
- Interfaces - Data
Package - Classes - Methods
• The API is divided into packages 6
Java defined core packages
• Java has a few core packages
• Each serve a different purpose
• Examples include:
• java.lang: for Java’s fundamental classes
• java.util: for collections framework and utility
classes. e.g. Date, Random number generator, etc
• java.io: for file input and output related classes
• etc (See Java API documentation)
Using packages
● Add import keyword before class definition
● Syntax:
import packageName.className;
● Note
• java.lang package is included by default in
Java
8
Example: using a built-in package
• Remember Scanner class?
• We used this to read input, e.g. from keyboard
• It is in the java.util package
How have we been importing Scanner class
a) import java.util.*; // will import all classes in util
*: means import all classes in this package
b) import java.util.Scanner; // will import Scanner only
● Which Scanner methods have we used?
• next(), nextInt(), nextLine(), hasNext(), etc 9
b) User-defined packages
● We can create our user defined classes & package them
• i.e. create user-defined packages
• Use their classes and methods in many programs
Steps?
a) Create the necessary directory structure
• src directory
*.java files
• classes directory
All class files and packages go here 10
b) Insert keyword package followed by package name
- MUST be 1st line in ALL classes to include in the package
• Syntax
package package_name;
• Note
• Class should be a user-defined one
• i.e. MUST NOT have main()
• Recommendation:
• Use lower case letters for package names
• To avoid name conflicts with classes
illustration
package pkg; package pkg;
public class A{ public class B{
// data & methods // data & methods
} }
• Save as A.java • Save as B.java
Note:
◦ Both classes have package pkg added at the top
◦ They will be added to package pkg
12
c) Create the package
• Compiling each class to include individually
How?
javac –d classes/ -sourcepath src src/…/class_name;
Note:
• Working directory MUST be one above src and classes folder
• -d: for creating a directory with given package_name
• classes: for creating package directory in the classes directory
• alternatively: type . (dot) to create package directory in working
directory)
// Upon successful compilation:
Classes directory will have a class file for each compiled Java file
d) Insert keyword import keyword,
followed by package_name.class_name in
Tester class
• Syntax
import package_name.class_name;
Rem
import java.util.Scanner; ?
e) Test your package
How?
● By executing the Tester class:
● java –classpath classes
packagefolder.Tester_name;
Note:
● You must work from folder above classes
folder
Summary
• Packages
• Built-in packages
• User-defined packages
Next lesson
• Sorting Arrays
17
Q &A
16