-
Notifications
You must be signed in to change notification settings - Fork 4
Java Packages
Anthony Christe edited this page Nov 6, 2013
·
1 revision
Notes adapted from Oracle's tutorial
Imagine you had a bunch of classes that represent graphics objects which also implement a Draggable interface
//in the Draggable.java file
public interface Draggable {
...
}
//in the Graphic.java file
public abstract class Graphic {
...
}
//in the Circle.java file
public class Circle extends Graphic
implements Draggable {
. . .
}
//in the Rectangle.java file
public class Rectangle extends Graphic
implements Draggable {
. . .
}
//in the Point.java file
public class Point extends Graphic
implements Draggable {
. . .
}
//in the Line.java file
public class Line extends Graphic
implements Draggable {
. . .
}- These types are clearly related
- They should be combined in a way that shows they're related
- Other programmers should be able to find related objects easily
- The types should have unrestricted access to each other, yet still be restricted outside of package
- A package is a grouping of related types providing access protection and namespace management.
- Make types easier to find and use
- Avoid naming conflicts
- Control access
- Bundle groups of related types into packages
- I.e. mostly useful for organization
- You've already been working with packages (java.util.LinkedList, javax.swing.* , etc)
- The keyword package is used as the first item of a source file
- The name of the package follows the keyword
- Each source file may only declare one package
- The package declaration must be the first item in the source file
- Source files must be stored in directories that represent the package
package edu.achriste.graphics
public class Polygon implements Draggable {
...
}The above source file would be stored in the file hierarchy as edu/achriste/graphics/Polygon.java
- All lower case to avoid conflicts with classes or interfaces
- Companies use reverse internet domain to start there packages
- Packages in Java language begin with java or javax
org.apache.commons.math
edu.hawaii.achriste.ics211.a11- Useful for infrequent use
java.util.List<String> list = new java.util.LinkedList<String>();import java.util.LinkedList;
import java.util.List;
List<String> list = new LinkedList<String>();- Generally considered bad style
- Sometimes useful when working with GUI libraries (many imports)
import java.util.*;
List<String> list = new LinkedList<String>();- This type of import only imports types in package, but will not import other packages
- import java.awt.* will import these types
- Note that in the above import, classes from java.awt.color and java.awt.fonts are not imported
- Types with the same name from different packages can be imported
- Use fully qualified name for type that you want to use
- Static methods and fields can be imported using static import
- Great for constants or methods that are used repeatedly
- Often used for assertions in JUnit tests
double r = Math.cos(Math.PI * theta);versus
import static java.lang.Math.PI;
import static java.lang.Math.cos;
double r = cos(PI * theta);- Easiest to let IDE handle it
- Use a build system (ant, maven, etc)
- For each package, add all packages to compile command
javac edu/hawaii/achriste/graphics/* edu/hawaii/achriste/utils/*
- To run a program in a package, run class with main using fully qualified name
java edu.achrist.graphics.Main