≡ Menu

Access Modifiers in Java

You have seen Java modifiers in code over and over again, but we’ve never really talked about them in-depth. So what better time than now, right?

What are Java Modifiers?

Also called access modifiers, these are seen in code as public, package, protected and private. These keywords exist in Java as a means for encapsulation, I’ve touched on this briefly back in my post about object-oriented programming. These access modifiers will either allow or deny other Objects from being able to execute the code that is wrapped by the keywords.

What do I mean when I say that it will allow or deny access? I mean that Java will either let you access whatever Class or method you wish to by allowing the code to compile, or if you’re not allowed, your code will simply not compile! So then, if your code won’t compile because you don’t have the access to another Class or method, then what’s stopping you from going into that Class or method and changing the access modifier to be public? Well, nothing is stopping you… but this could lead to potential problems in the future.

Programmers restrict access to certain areas of code on purpose, because they don’t want other parts of the code to interfere with their Objects. Remember that Objects can interact with each other and change each other’s variables. If I was a user in Facebook, I shouldn’t have access to change another Facebook user’s password right?

Examples of Access Modifiers in Java

Let’s look at some code, I’ve put together a little program that revolves around houses and neighbourhoods. There is a YourHouse Object that represents your house (duh). There’s a YourParentsHouse Object as well as a YourNeighboursHouse Object. I’ve created a bunch of methods inside of each of these Objects that represents some actions that you may take on a regular day. The example I’ve outlined is if you lost your keys, and are trying to get into someone’s house for help.


package com.howtoprogramwithjava.runnable;
import com.howtoprogramwithjava.yourneighbourhood.YourHouse;
public class AccessModifiersProgram
  public static void main (String[] args)
    YourHouse yourHouse = new YourHouse();


package com.howtoprogramwithjava.yourneighbourhood;
import com.howtoprogramwithjava.yourparentsneighbourhood.YourParentsHouse;
public class YourHouse extends YourParentsHouse
  protected void enterYourHouse ()
    System.out.println("Entering " + getClass().getSimpleName());
  public void knockOnDoor ()
    System.out.println("You knock on the door of " + getClass().getSimpleName());
    double randomNumber = Math.random();
    if (randomNumber >= 0.5)
      System.out.println("You are greeted at " + getClass().getSimpleName());
      System.out.println("No one answers");
  protected void goToParentsHouse ()
    System.out.println("You are going to YourParentsHouse");
    if (!this.enterYourParentsHouse())
      YourNeighboursHouse yourNeighboursHouse = new YourNeighboursHouse();


package com.howtoprogramwithjava.yourparentsneighbourhood;
public class YourParentsHouse
  protected boolean enterYourParentsHouse ()
    if (Math.random() >= 0.5)
      System.out.println("Entering YourParentsHouse");
      return true;
      System.out.println("No one is home at YourParentsHouse");
      return false;
  private void changeThermostat ()
    System.out.println("Only your parents can change their thermostat!");


package com.howtoprogramwithjava.yourneighbourhood;
class YourNeighboursHouse
  void enterYourNeighboursHouse ()
    System.out.println("Your neighbour is home, so you enter " + getClass().getSimpleName());

Okay, so we’ve got a lot going on here. One piece of code you may not recognize is the Math.random() static method. What’s that about? It’s a really cool method that will randomly generate a number between 0.0 and 1.0. Why is that helpful? Well it means I can say that a certain event will happen if the number is between 0.0 and 0.49, and if it’s bigger than 0.49, then do something else. This is what I’m doing in my example code above. So if you were to copy/paste the code into your own project, you’ll see that the outcome in the console changes almost every time you run the code! Neat!

Anyway, let me list out the important things you should observe in this code:

  • YourHouse extends YourParentsHouse
  • YourNeighboursHouse doesn’t use any modifier keywords
  • YourParentsHouse has a private method

Alright, so since YourHouse extends YourParentsHouse this means that YourHouse will have access to all of the public and protected methods inside YourParentsHouse, as well as all of the public, protected and private methods within it’s own code.

What can we learn from this?

public (Objects and Methods)

If a method is declared as public, then every Object or method that exists inside of any package in your program will have access to that method. This is equivalent to you leaving the front door of your house WIDE OPEN to the public. You’re inviting everyone and anyone in to snoop around 😉

Note: Just because your Object is declared as public doesn’t mean that every other Object or method has access to every aspect of your Object… as we can still modify the access levels of the methods within your Object.

protected (methods only)

If your method (within your Object) is declared as protected, this means that the only Objects/methods that will have access to your method are those that extend your Object (or Objects that your Object extends). In other words, any Objects in your Object’s inheritance chain, will have access to your Object’s protected method.

This is seen when YourHouse calls the protected boolean enterYourParentsHouse () method. This method is protected, and it belongs to another Object (YourParentsHouse), but since YourHouse extends YourParentsHouse, then we’re all good!

package (Objects and methods)

This modifier is not used very often as there aren’t many practical applications for it. But for the sake of completion I’ll talk about it. This modifier can be observed in the YourNeighboursHouse Object. Look at the void enterYourNeighboursHouse () method. You’ll notice that there is no modifier keyword specified in front of the method declaration. This means that it’s a package level modifier. That’s right, you don’t actually put the keyword package into your code to specify that it should be a package level modifier. Kind of strange, but that’s how it’s implemented.

So when your method (within your Object) is declared as package, this means that only other Objects that belong to your Object’s package can see your method. You’ll notice that both YourHouse and YourNeighboursHouse belong to the package com.howtoprogramwithjava.yourneighbourhood. So this means that YourHouse will be able to access YourNeighboursHouse‘s package level methods. However, if I were to create a protected method inside of YourNeighboursHouse, then YourHouse would NOT be able to access this method! This is because YourNeighboursHouse is NOT inside of the inheritance chain of YourHouse. Neat!

private (methods only)

Finally we arrive at the last modifier. When a method is declared as private, this means that ONLY the Object that declares the method will have access to it. So this would mean that YourHouse will NOT have access to the private void changeThermostat () method inside of YourParentsHouse (even though they’re on the same inheritance chain).

Coding Exercise

So I want you to get a good understanding of what I’m talking about when I say that certain Objects have access to other methods. So I want you to copy/paste my code into your STS IDE program. I want you to navigate to the AccessModifiersProgram.java file and at the end of the main method, I want you to type in “YourHouse.”, you’ll see the code completion popup appear with a list of methods that you can access FROM the AccessModifiersProgram.java file. Make note of what methods you CAN see and what methods you CANNOT see. Now play around with the access modifiers for some of the methods inside of YourHouse and make some public (be sure to save the code). Now go back any type in “YourHouse.” in the main method again, and you’ll see something different. Do you understand why? I hope so! If not, try to figure out what you’re not understanding and ask me in the comments section!

Let’s summarize

Now that you have an understanding of what Java modifiers are, I’ll explain again why they’re important. If you were to design an entire web application using Java, and you decided to make everything public, you’ll quickly see bugs start to emerge as you have other developers work on your code. Using Java modifiers is a good thing and allows for a truly Object Oriented approach to your coding. So remember to design your applications with the real world in mind. If an Object has a method that just wouldn’t make sense for any other Object to mess around with, then make it private! This is good programming practice, as I’ve mentioned before, it’s called encapsulation – which sounds cool, so it must be good then right?

That’s all I have to say for now, so now it’s your turn to help me out. If you found this helpful, please share this article with your friends via the share bar on the left side of your screen. Like/tweet/stumble/g+ away ladies and gentlemen, and I’ll see you in the next tutorial!

{ 6 comments… add one }
  • Durim April 14, 2013, 11:04 am

    Thank you so much Trevor!

    Very good code example! I learned a lot from it 🙂

  • carolyn October 14, 2013, 4:37 am

    Thanks Trev, each lesson gets better

    • Trevor Page October 16, 2013, 7:46 am

      I’m thrilled that you’re enjoying these tutorials Carolyn!

      Best of luck to you and feel free to reach out here if you have any questions.

  • kashpharma October 21, 2013, 7:56 am

    Thanks a bunch trevor,your article was very helpful

  • javascript obfuscator September 9, 2017, 12:09 am

    Great lesson! Keep it up!

  • Arivazhagan March 4, 2019, 1:56 am

    Hi Trev,

    How about adding private modifier to class level? is there any use of doing it (or) is it applicable for any scenario?

    Thanks in advance.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.