The Importance of Access Modifiers

Articles —> The Importance of Access Modifiers

As a novice programmer many years ago, I remember finding few reasonable examples demonstrating the uses and advantages of access modifiers (public, private, protected, and package private modifiers in java)1. Quite often I would break the rules - doing things such as making variables public because it was easier to do so. With the short programs and scripts I wrote the consequences were minimal, and while I understood the concept of encapsulation, with these small scripts its importance never seemed to grab my attention enough to make use of its value. However, as soon as I started building larger applications and libraries, it became evident that my choice to occasionally break those rules was an idiotic move (a move which is embarrassing to admit). Why? Well for starters, choosing to make certain variable public meant any change to that variable - its data type or name - meant I must cascade those changes throughout all clients using that variable...it got ugly real quick.

A simple example is demonstrated below:


/*

* Simple Demo class illustrating access modifiers

*/

public class Demo{

    public static final int MAX = 10;

    public String[] string = new String[MAX];//instance variable declared public - bad practice



    public Demo(){

       for ( int i = 0; i < MAX; i++ ){

           string[i] = Integer.toString(i);

       }

    }



}

A simple class that stores ten strings in an array. Now, lets have a client access that array:


/*

* Client accessing the public variables of Demo

*/

public class Client{

    Demo demo = new Demo();

    public Client(){



    }

    public String getValue(int loc){

        return demo.string[loc];//access the public variable in demo

    }

}

Simple enough, and all is fine and dandy right? But what if we must change the array to something more dynamic - for example to an ArrayList? Now, we must make changes to all references to the public variable, and if these are accessed frequently, it starts getting ugly. But what if we're a bit more careful with our access modifiers, for example:


/*

* Simple Demo class illustrating access modifiers

*/

public class Demo{

    public static final int MAX = 10;

    private String[] string = new String[MAX];//private variables can only be accessed using public getters and setters



    public Demo(){

       for ( int i = 0; i < MAX; i++ ){

           string[i] = Integer.toString(i);

       }

    }

    /*

    * Retrives the values of the private string variable.

    */

    public String getValue(int loc){

        return string[loc];

    }



}

Here, we've made that public variable private. What we've truly done is allow ourselves great flexibility in how we can change those variables, doing so without directly effecting ANY client trying to access those values. What if we wish to change the data type of that String? What if those object values must become part of a more complex class? Encapsulating those values by using access modifiers allows one to make any of those changes necessary without effecting clients accessing those values. What we've truly done is abide by a basic principle of code design: encapsulate what changes.

While using access modifiers is a very basic principle, I myself have, and quite often I see newcomers break this very simple rule. One must understand why there is a rule, and what the importance of variable hiding truly entails. Without this understanding, there isn't much incentive to follow the rules.

1 Access Modifiers in Java


Comments

  • Priya Arrora   -   March, 24, 2018

    Java is one the mature and established programming language, it keeps evaluating that enhance its efficiency.

Back to Articles


© 2008-2022 Greg Cope