Sub topic 4
Access Modifiers for Classes and Members
A Java class has its own access level. Its members also have access levels. A member is a field, method, or nested class declared inside a class. A constructor is not a member, but it has an access level too.
These levels work together. For example, a public method is not useful to a caller that cannot access the class containing it.
Top-level classes
A top-level class is a class declared outside every other class. It can have one of two access levels:
public: Code in other packages can use the class, subject to other access rules.- No access modifier: The class is package-private. Only code in the same package can use it.
A package is a way to group Java classes. If a source file declares a public top-level class, the file name must match the class name. For example, public class Account must be in Account.java. A file can have at most one public top-level class.
A top-level class cannot be private or protected.
Nested classes and other declarations
A nested class is a class declared inside another class. It has more access choices than a top-level class.
| Declaration in an ordinary class | Allowed access levels |
|---|---|
| Top-level class | `public` or package-private |
| Nested class | `public`, `protected`, package-private, or `private` |
| Field or method | `public`, `protected`, package-private, or `private` |
| Constructor | `public`, `protected`, package-private, or `private` |
The protected level allows access within the same package and can also allow access in a subclass. A subclass is a class that extends another class. Access from a subclass in a different package has extra rules.
A class declared inside a method is called a local class. It cannot have an access modifier.
Walk through an example
Save this code as AccessDemo.java:
public class AccessDemo {
public static void main(String[] args) {
Box box = new Box("Books");
System.out.println(box.getLabel());
Box.Tag tag = box.makeTag();
System.out.println(tag.text());
}
}
class Box {
private final String label;
Box(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public Tag makeTag() {
return new Tag(LabelMaker.make(label));
}
public static class Tag {
private final String text;
private Tag(String text) {
this.text = text;
}
public String text() {
return text;
}
}
private static class LabelMaker {
static String make(String label) {
return "Tag: " + label;
}
}
}In a terminal, run:
javac AccessDemo.java
java AccessDemoThe program prints Books on the first line and Tag: Books on the second.
AccessDemo is public, so its name matches the file name. Box is package-private. It can share this file because it is not public. The two classes can use each other because they are in the same package.
The label field is private. Code in AccessDemo cannot read it as box.label. Instead, it calls the public getLabel method.
Follow the access path
Tag is a public nested class of Box. The name Box.Tag tells Java which class contains it. The word static means this nested class does not need its own enclosing Box object to exist.
Even so, a caller must be able to access Box before it can use Box.Tag. If Box were in a named package, code in another package could not refer to Box.Tag, because Box is package-private.
The Tag constructor is private. The enclosing Box class can call it, but AccessDemo cannot call new Box.Tag("Books") directly. Instead, AccessDemo calls box.makeTag() and receives a tag.
LabelMaker is a private nested class. Box uses it to make the tag text, but AccessDemo cannot use Box.LabelMaker. Its make method is not public either. The static method can be called on LabelMaker without creating a LabelMaker object.
Exercise
Exercise
In the example, why can AccessDemo call box.makeTag() but not new Box.Tag("Books")?
Show answer
Box and its public makeTag method are accessible in the same package. Tag is also accessible there, but its constructor is private. Box can call that constructor; AccessDemo cannot.