Java Language Security Constructs

In this chapter, we’re concerned primarily with how Java operates on things that are in memory on a particular machine. Within a Java program, every entity -- that is, every object reference and every primitive data element -- has an access level associated with it. To review, this access level may be:

private

The entity can only be accessed by code that is contained within the class that defines the entity.

Default (or package)

The entity can be accessed by code that is contained within the class that defines the entity, or by a class that is contained in the same package as the class that defines the entity.

protected

The entity can only be accessed by code that is contained within the class that defines the entity, by classes within the same package as the defining class, or by a subclass of the defining class.

public

The entity can be accessed by code in any class.

The notion of assigning data entities an access level is certainly not exclusive to Java; it’s a hallmark of many object-oriented languages. Since the Java language borrows heavily from C++, it’s not surprising that it would borrow the basic notion of these access levels from C++ as well (although there are slight differences between the meanings of these access modifiers in Java and in C++).

As a result of this borrowing, the use of these access modifiers is generally thought of in terms of the advantage such modifiers bring to program design: one of the hallmarks of object-oriented ...

Get Java Security, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.