Software Development Packages Computer Science 209 Software Development Packages
What Is a Package? A package organizes related resources (interfaces and classes) into a conceptual unit Like a module in other languages Examples: java.lang (basic Java resources, no need to import) java.util (scanners, collections, arrays, random numbers) java.io (I/O, file processing) javax.swing, java.awt (GUIs)
Using a Package: Typical Imports import java.util.Scanner; import java.util.Arrays; public class CircleArea{ public static void main(String[] args){ int[] numbers = new int[3]; Scanner input = new Scanner(System.in); for (int i = 0; i < numbers.length; i++) numbers[i] = input.nextInt(); Arrays.sort(numbers); for (int n : numbers) System.out.println(n); } Bring in specific classes only
Using a Package: Fully Qualified Imports import java.util.Scanner; import static java.util.Arrays.sort; public class CircleArea{ public static void main(String[] args){ int[] numbers = new int[3]; Scanner input = new Scanner(System.in); for (int i = 0; i < numbers.length; i++) numbers[i] = input.nextInt(); sort(numbers); for (int n : numbers) System.out.println(n); } Bring in specific classes and static methods only
Using a Package: The Wildcard Gets All import java.util.*; public class CircleArea{ public static void main(String[] args){ int[] numbers = new int[3]; Scanner input = new Scanner(System.in); for (int i = 0; i < numbers.length; i++) numbers[i] = input.nextInt(); Arrays.sort(numbers); for (int n : numbers) System.out.println(n); } Bring in all classes with wildcard
Using a Package: No Imports Needed public class CircleArea{ public static void main(String[] args){ int[] numbers = new int[3]; java.util.Scanner input = new java.util.Scanner(System.in); for (int i = 0; i < numbers.length; i++) numbers[i] = input.nextInt(); java.util.Arrays.sort(numbers); for (int n : numbers) System.out.println(n); } Use fully qualified names with no imports
Packages and Files In Python, a module is typically a single file, which may define several related classes In Java, a package is a directory, which may include several related classes in source code files and byte code files
File Organization within a Package Source files for classes should be in a directory whose name is the package name When byte code is generated, it is placed in a directory with the package’s name These directories should be just below the directories of the source or byte code files that use them
Example: The Student Class studentexample StudentTester.java StudentTester.class student Student.java Student.class
Defining a Package Define Use package student; public class Student{ // Blah blah blah } Use import student.Student; public class StudentTester{ // Blah blah blah }
Creating a Package from the Terminal Place the .java files in a directory whose name is the package name Attach to this directory and run javac *.java Place the package directory in the directory where your client program will be Run javac or java from this client program’s directory as needed
All Classes in One Package student StudentTester.java StudentTester.class Student.java Student.class
Defining a Package Define Use package student; public class Student{ // Blah blah blah } Use package student; public class StudentTester{ // Blah blah blah }
Compile and Run > cd student > javac *.java > java student.StudentTester Need qualified name to locate main class within package