The basics of Java access modifiers

Photo by Jason Pofahl on Unsplash

Access is an important element of encapsulation in object-oriented programming. For example: can an instance of a given class access the fields of an instance of another class? That depends on what access level that particular field has been designated with.

Java has four access levels for class fields and “methods”: public, protected, package private and class private (usually called just “private”). I’ve listed these in order from least restrictive to most restrictive.

The module system introduced in Java 9 allows for even more fine-grained control, but I really won’t be covering that in this article. I assume that you’re either using Java 8 or you’re using Java 9 or later with all your classes in the same module, such as the default unnamed module.

So, if you’re using Java 9 or later with multiple modules, please imagine all further mention of public access to have the appropriate caveat about things that are not exportable to other modules.

As I understand them, modules are sort of super-packages. Everything that is declared public within a module can be accessed anywhere within that module. The visibility of a module’s public items to other modules is a topic I leave to someone else to explain.

Public items can be accessed outside the packages they’re defined in. Package private items can only be accessed within that package. Protected items can be accessed anywhere within the same package, and also outside of the package, but then only by subclasses.

Public corresponds to the reserved word public, protected is marked with protected and class private is marked with private (since package private is the default, it has no access modifier).

It almost goes without saying that there can only be one access modifier per declaration. It is, for example, impossible for something to be both public and private. A repeated modifier also causes a compilation error.

But, as long as the modifiers’ meanings are not contradictory or inapplicable, modifiers can be freely combined. There is, for example, no particular reason why a private function can’t also be in strict floating point mode (with the modifier strictfp).

Nor do the modifiers have to be in any particular order, though it’s customary to place an explicit access modifier before any other modifier that might apply, e.g., “public static final” rather than “final public static.”

Do note, however, that types are not modifiers. Something like “static int final public” would not compile. Modifiers first, type and then identifier, that’s the order they go in.

In this article, I’m only going to use toy examples. Here I’m more interested in giving an example of every possible situation than in giving a realistic example of how you might use access modifiers to enforce encapsulation in a real world project.

Here’s our first toy example:

The ToyExample class per se can only be public or package private. By default, NetBeans makes new classes public and IntelliJ generally makes new classes package private. You can change those on a case-by-case basis, and you can probably also change the default.

It is quite typical for a constant like INTEGER_CONSTANT to be public and a variable like instantiationCount to be private. Then it may or may not be necessary to provide a public getter for the private variable.

Protected variables like someInteger and package private variables like someNumber have their uses, though perhaps more so in the earlier days of Java than nowadays.

We are generally more inclined nowadays to make variables private and provide getters and setters for the variables that need them.

But back in the days when Java was thought to be very sluggish compared to even unoptimized C++, it’s understandable that programmers would have worried that accessing a field through a getter or setter might incur an unacceptable performance penalty.

Thus they would have been reluctant to make every field class private. If there is still a performance penalty for accessing fields through getters and setters, it’s usually so small as to not be worth worrying about.

To illustrate the access of some of these ToyExample fields, here’s an example in a different package, the poorly named various package:

Of course AccessDemo’s main() can access ToyExample’s static constant INTEGER_CONSTANT, since it’s declared public. It can be accessed from any package, subject to the module caveat. The same goes for the static getter getInstantiationCount().

On the other hand, the protected, package private and class private fields of ToyExample are definitely inaccessible to AccessDemo’s main(). The lines trying to access someInteger, someNumber and instantiationCount all cause compilation errors.

Note that easyExample is a local variable, with its scope is limited to main(), and so it doesn’t need an access modifier. Nor will the compiler allow an access modifier for it (though your IDE might suggest you convert it to a field).

Although serialVersionUID is marked private, it can actually be accessed outside of the package. But that’s because of so-called “native methods.” If there are no such platform-dependent subroutines trying to access and of ToyExample’s private fields, the private access modifiers will be respected.

The same access modifiers can be applied to “methods.” And if you can call it, you can override it, provided of course it’s not marked final. I’ve added a few functions and procedures to ToyExample to illustrate.

Of course toString() is inherited from Object, and it’s expressly meant to be callable from any class and overridable by any class you might want to write.

Trying to narrow the access level for toString() will cause a compiler error. You may mark a toString() override final, though I can’t imagine why you’d ever want to do that, other than to see that you can.

However, it does make sense to mark getInstantiationCount() final, so I did. It can be called from any class, but it wouldn’t make sense for ToyExample subclasses to override it. Given that instantiationCount is private, a subclass can’t meaningfully override getInstantiationCount().

If you’ve been following along in your IDE, I would like you to create two ToyExample subclasses, one in the same package, and another one in a different package.

I’m gonna name mine SamePkgSub and DiffPkgSub, and place the latter in the same poorly named various package as AccessDemo. Also, I’m going to add a couple of overrides to SamePkgSub:

This class can’t override getInstantionCount() even though it’s public, because it’s also final. But both adjustInteger() and adjustNumber() are fair game, even though the former is protected and the latter is package private.

I’m going to leave makeNumbersEqual() alone, it can’t be overridden even though it’s not explicitly marked final: it’s effectively final. You can certainly add “final” in there if you want, but your IDE should give you a warning for the essentially redundant modifier.

Now try copying both overrides to the different package subclass. The adjustInteger() override should be fine, but adjustNumber() should cause an error (or two errors, if you include the override annotation).

Neither of those can be called from AccessDemo. It’s up to you whether you want to keep or delete what you previously had in AccessDemo’s main(), if you’re following along.

The constructor of a class doesn’t have to have the same access level as the class itself. The constructor can have any of the four access levels. Though in my opinion it doesn’t make much sense for a package private class to have a public constructor. The compiler will allow it, though.

By contrast, it can be very useful for a public class to have only a private constructor. This may be done to restrict instantiation. The class might provide a static function, typically called getInstance(), as the only means for obtaining instances of the class.

This is similar to what happens with enumerated types. You may write a constructor for an enumerated type, but it must be private, though you may omit “private” from the declaration.

The DayOfTheWeek enumeration is not a realistic example because for most purposes it makes more sense to use java.time.DayOfWeek than to reinvent this particular wheel.

Even though the DayOfTheWeek constructor appears to be package private, it’s actually class private, and your IDE will show it with the same padlocked icon as private constructors in regular classes (at least NetBeans does in its Navigator pane).

The java.time.DayOfWeek enumeration doesn’t have an explicit constructor. The compiler will nevertheless always make a sole private constructor for all enumerated types, based on the sole protected constructor for Enum<E>.

And if you do write an explicit constructor for an enumerated type, the compiler will put in bytecode equivalent to the appropriate super() call (you can try to write that yourself, but it will be flagged as an error).

Of course a Java class that is not an enumerated type may have multiple constructors. And it’s even possible to have at least one constructor for each of the access levels.

For the next example, let’s add these three fields to ToyExample:

Then let’s write these four constructors:

I emphasize again that this is a toy example. It is likelier, in a real world project, to have multiple constructors for only one or two access levels, like maybe just public and private.

Even the String class is an example of this. Although it has something like two dozen constructors, almost all of them are public and one of them is package private (I’m looking at the Oracle JDK for Java 8).

Maybe in another JDK version there might be a private constructor or two for String, but I doubt you’ll find one with a protected constructor. That’s because, you see, String is marked final, it can’t be subclassed.

You might think that a class marked final is not allowed to have a protected constructor. I thought the same thing. But, as it turns out, the compiler allows it. In such a case, the protected constructor ends up being quite similar to a package private constructor.

So, regardless of whether or not a class is marked final, you can write constructors at each access level that you want, but in practice you should only write constructors at each access level your class needs.

Since AccessDemo is not a subclass of ToyExample, and they’re in different packages, it would seem that the rather limited public ToyExample constructor is the only ToyExample constructor which AccessDemo can use.

Actually, AccessDemo can also use the protected constructor without AccessDemo subclassing ToyExample, by using an anonymous subclass inline.

As you can see, anonymousExample can use the protected ToyExample constructor and its hashCode() override can access the protected ToyExample field someInteger.

But outside of the anonymousExample declaration, AccessDemo can’t access the someInteger field of any ToyExample instance.

Of course the anonymous subclass defined for anonymousExample is purely local. The anonymousExample instance can be passed outside of AccessDemo, but it would more likely be understood as a ToyExample instance (assuming this class had any practical usefulness).

For what it’s worth, I don’t like anonymous classes too much. I think they’re too often used for things that can be reused (such as file choosers that ask the user to confirm before overwriting existing files), leading to unnecessary duplication in the project.

Even nested classes are more amenable to reusability than anonymous classes. Even if the nested class is marked class private. They’re also more amenable to testing, if they’re not marked class private.

The terminology of nested classes is a little confusing, but when it comes to choose an access modifiers for a nested class, it doesn’t actually matter whether the nested class is “static” or not. All four access levels are possible for a nested class.

However, do note that a nested non-static class can access the private fields of the containing class, as the following example demonstrates:

(I cleared out some things from prior examples.)

The counter referenced by revealCounter() in ToyExample.NestedNonStatic is the counter marked as a private ToyExample field. But access to that private field is denied to ToyExample.NestedStatic. In some ways, a nested static class is no different from an outer class.

By the way, you can nest an interface inside of a class or inside another interface. But unlike a nested class, an interface nested inside another interface can’t be protected or private.

However, an interface nested in a class may be protected or class private. I guess this makes sense. I didn’t even know this could be done at all, so I won’t give an example of this, practical or not.

With multiple nesting levels, the combinations of access modifiers can get very complex. But that complexity’s not worthwhile, in my opinion.

Like outer classes, outer interfaces can either be public or package private. Up to Java 7, there was only one logical choice of access modifier for interface functions and procedures: the exact same as the interface itself.

Consider this toy example:

Since Java 7 interfaces can’t provide implementations, it’s unnecessary to mark supply() as abstract. It’ll still compile, but IntelliJ gives a warning.

Java 8 does allow interfaces to contain implementations, but, perhaps mostly so as to not break older programs, does not require the “mandatory methods” to be marked abstract.

So IntelliJ also considers “abstract” to be redundant for Java 8 interfaces, and will offer to remove all redundant modifiers.

This leads to somewhat of an inconsistency between classes and interfaces: the absence of an access modifier in a class always means package private, but in an interface it always means public… except on the declaration line (the line with the reserved word “interface”).

I hope this has either been a good introduction or a welcome refresher. But I haven’t said anything about when you should choose a particular modifier and why. I’m going to write a separate article about that.

The use of toy examples can exacerbate a certain feeling of pointlessness, a feeling that you have many fine-grained options for access levels, but no practical reason to take full advantage of them. That’s what I will address in the next article.

is a composer and photographer from Detroit, Michigan. He has been working on a Java program to display certain mathematical diagrams.