The Complexity of Java Access Specifiers

By Xah Lee. Date: . Last updated: .

The rise of the elaborate access specifiers and their different consequences in Java entities is a complexity out of OOP and its machinery.

〔if you are not familiar with Java's access specifiers, see: Java: Access Specifiers

Note that the concept of access specifiers such as “private” is not at all related to issues of secrecy or the all-important aspect of cryptography in computing. Compiled Java code can relatively easily be decompiled by compiler specialists.

The concept of access specifiers is arisen out of the perceived need of good engineering practices. More specifically: to provide a way in the language of OOP paradigm to specify and control what items can not be accessed.

This concept and scheme, is not relevant to some 95% of programing in the real world. In writing Java code, you don't have to fret over them. Nothing's gonna happen if you declare everything public. Just about every other programing language are all “public”.

As we've seen above, these “access specifiers” has different meanings when used on different Java entities. When used on class variables or methods, they control where these variable/methods can not be used. When used on constructors, they control where a class can not be instantiated. And when applied to classes, they declare which class cannot be seen. How exactly did these complexities arise?

To find the answer, we realize that all these differences, including the concept of access specifiers, is a immediate consequence of OOP. As we've seen in the article OOP's Jargons and Complexities, classes and methods are really just subroutines and inner-subroutines. And, a instance of a class (objects), are really just variables assigned with such super-subroutines as values. Naturally, when one deals with super-subroutines and execute commands by assigning them to variables and calling inner-subroutines thru these variables, complexity ensues.

Normally, the scoping of variables can be classified as one of dynamic or lexical, or that of global and local. Now with OOP ways, a variable in a class has a scope, while the same variable when the class is instantiated (a objet) is a different scoping issue. Also, the scope concept applicable to variables is now broadened to methods (inner-subroutines) and constructors (specialized inner-subroutines). All in all, the scoping complexities of OOP as applied to different OOP entities is manifested as access specifiers in Java.