Sub topic 2
Packages and Where Access Occurs
A package is a named group of Java classes. Packages help organize code and give classes a place to live. They also matter for access: code in the same package can sometimes use things that code in another package cannot.
A class belongs to the package named at the top of its source file. A source file is a text file containing Java code. For example:
package school.tools;
public class Label {
}Here, Label belongs to school.tools. The full name of the class is school.tools.Label.
Location matters
A member is a field or method that belongs to a class. A method is a named set of instructions a class can run.
When code uses a member, Java checks whether access is allowed from the place where that code is written. For example, a method with no access word before it has package-private access. Other classes in the same package can use it, but classes in a different package cannot.
The word public makes a class or member available more widely. You will learn the access rules in more detail in later chapters. For now, focus on the package where the code appears.
Work through an example
This example has two classes in school.tools and one class in school.app.
Create these files inside a new working folder:
school/tools/Label.javaschool/tools/ToolsDemo.javaschool/app/App.java
The folders match the package names. In each file, the package declaration comes before the class.
Put this code in school/tools/Label.java:
package school.tools;
public class Label {
static String prefix() {
return "Note";
}
public static String forText(String text) {
return prefix() + ": " + text;
}
}prefix has package-private access because it has no access word. forText is public.
Both methods use static. A static method belongs to the class itself, so code can call it without first creating an object.
Put this code in school/tools/ToolsDemo.java:
package school.tools;
public class ToolsDemo {
public static void main(String[] args) {
System.out.println(Label.prefix());
}
}The main method is where Java starts this program. System.out.println displays a line of text.
ToolsDemo can call Label.prefix() because both classes are in school.tools.
Now put this code in school/app/App.java:
package school.app;
import school.tools.Label;
public class App {
public static void main(String[] args) {
System.out.println(Label.forText("Read"));
}
}An import lets this file use the short name Label instead of writing school.tools.Label each time. It does not move the class into school.app or change its access.
App is in a different package, so it calls the public method forText. That method calls prefix from inside Label, where the call is allowed.
Run the example
You need a Java Development Kit, or JDK, installed. The JDK includes javac, the Java compiler. A compiler translates source code into a form Java can run.
Open a terminal in the working folder that contains school. Run:
javac -cp . -d . school/tools/Label.java school/tools/ToolsDemo.java school/app/App.java
java -cp . school.tools.ToolsDemo
java -cp . school.app.AppThe -cp . option tells Java to look in the current folder for classes. The compiler option -d . puts the compiled files under that folder. The output is:
Note
Note: ReadNow try changing the call in App.java from Label.forText("Read") to Label.prefix(). Run the compile command again. The compiler reports an access error because App is not in school.tools.
Change the call back before continuing. Adding an import would not fix the error: imports make names easier to write, but they do not grant access.
Packages are more than folders
The package declaration tells Java which package a class belongs to. The folder layout helps the compiler and Java find the files.
A file without a package declaration belongs to an unnamed package. For programs with several classes, named packages make the code easier to organize and refer to.
Exercise
A class in school.tools.extra tries to call Label.prefix(). Is it in the same package as Label? Can it call the method?
Show answer
No. school.tools.extra and school.tools are different packages. The call is not allowed because prefix has package-private access.