Packages - Utah Valley University
Download
Report
Transcript Packages - Utah Valley University
Packages
Access Specifications
• Public
• Available anywhere (public keyword)
• Only one public class per file allowed
• Protected
• Available in subclasses, and in the package
• protected keyword
• Package (“Friendly”)
• Available in the package only
• No access specifier
• Private
• Available in class only (private keyword)
What’s a Package?
• A set of classes (.class files) in the same
directory
• Similar to C++ namespaces
• Don’t have to use import
• A “substitute” for friend in C++
• The directory must match the package
name
• Can have multiple segments (dot-separated)
• java.util corresponds (loosely) to the
subdirectory java/util
• Use the package keyword
• Must be first non-comment line of file
The Class Path
• A search path
• Consists of directories or JAR files
• All package directories must be
subdirectories of a directory in the class
path
• -classpath option
• CLASSPATH environment variable
The Unnamed Package
• The “default” package
• All classes not in a named package
are in this package
• Can span multiple directories
• Uses the classpath
JAR Files
• “Java Archive”
• A way of collecting/compressing .class
files
• For faster loading of applets
• And for distributing pre-compiled
software
• Can run from command-line with the
“java” command
• java –jar MyJar.jar