Java, Software Development

Java Classes And Access Modifiers – Review

Java classes, along with its variables and methods, can use access modifiers – private, public, protected, and package-level (no keyword). These access modifiers set the visibility of the classes, their constructors, and instance or class variables, and methods to the other parts of the application codes.

Top-Level Java Classes

The TopLevelClass1 class is a top-level class. It is the only public class in a .java file that has the same name –

Let’s say we added another public class in the same file. Now, the codes will not compile.

The IDE or the javac command gives out the following compile-time error.

Similarly, if we had other classes that are private and protected, the codes will fail compilation.

For the private class, the compiler generates the following error.

For the protected class, the compiler generates the following error.

However, if we only had package-level classes (those with no access modifier before the class keyword), everything works – no compilation errors.

In summary, a top-level class is a Java class that is not within any other block of codes. Moreover, it has the same name as the .java file where it is stored. Also, it can only be either a public or a package-level class. The rules also apply to Java interfaces – on later posts.

From here on, we interchangeably refer to a Java class’ instance and class variables as properties and the methods as behaviors.

Default Constructors Access Modifiers

Default constructors are constructor the compiler inserts into our class codes during compilation when we don’t create our constructors. A default constructor’s default access modifier is the class’ access modifier.

Consider the following package-level class.

For a class that has a package-level access modifier, its default constructor will have the same access modifier.

The Four Access Modifiers in Java

There are four access modifiers that we can use on classes, both instance, and static variables and methods. They set the visibility of the class itself and its variables and methods to the other parts of the program. If they aren’t visible, the other codes won’t be able to use them, and the compilation will fail. In Java, there are four access modifiers.

The private Access Modifier

The private access modifier makes an instance or class variables and methods visible only to the codes within the class. Consider the following Java class that has both private property and method which are not visible to other codes.

Now, we have the StartupClass class that uses the TopLevelClassForPrivateAccessModifier class’ property and method.

When we run these codes, we will get compilation errors.

The TopLevelClassForPrivateAccessModifier class’ property and method are not visible to the StartupClass class. On the other hand, if we moved the main method to the TopLevelClassForPrivateAccessModifier class and run it, the codes compile, and the application runs successfully.

The public Access Modifier

The public access modifier makes the class itself, its instance or class variables and methods visible to all other codes of the application. Consider the following new class.

Next, we modify our StartupClass class to use the new Java class.

The codes compile and run successfully.

The no-keyword Package-Level Access Modifier

The package-level access modifier does not have a keyword compared to the other access modifiers. With the package-level access modifier, the class’ constructors, properties, and methods are visible to codes in the same package. Consider the following class in package com.turreta.accessmodifiers.package1.

Our modified StartupClass class is in a different from that of TopLevelClassForProtectedAccessModifier.

When we compile these codes, we get the following errors.

Suppose we moved StartupClass to the com.turreta.accessmodifiers.package1 package, the codes will compile and run successfully.

The protected Access Modifier

The protected access modifier works like the package-level access modifier. In addition to that, it works with inheritance and bypasses the package-level access restriction. Consider the following classes.

The  ProtectedAccessModifierClass class has a protected property and a protected method. Then, we create another class that extends ProtectedAccessModifierClass.

Although  ChildProtectedAccessModifierClass is in a different package from that of ProtectedAccessModifierClass, the child class has access to the parent class’ property and method via inheritance.

If we had another class that accesses the ProtectedAccessModifierClass class’ property and method, that class would fail compilation.

The Case Of Inner Classes

We can nest Java classes. If we top-level classes, we also have inner classes that we define within other classes. These classes can be either instance or static classes.


This post is part of a reboot Java tutorial.

You Might Also Like