You are currently viewing Understand Access Modifiers in Java

Understand Access Modifiers in Java

1. Introduction

In this article, you will learn the different access modifiers available in Java and how to effectively use them in your programs.

2. Prerequisites

To complete the following tutorial, you will need :

  • The Java SE Development Kit 17 (JDK 17): If you don’t have it already refer to this tutorial for the installation procedure.
  • IntelliJ IDEA: If you don’t have it already refer to this tutorial for the installation procedure.

3. What is an Access Modifier?

Access modifiers in Java are keywords that are used to control the use of the methods, constructors, fields, and methods in a class. Basically, they will determine two things:

  • Classes, methods, and attributes that you can call directly from your code
  • The scope of the code you write. That is, which other class or method has the right to call your code directly.

Access modifiers are a powerful way to ensure one of the founding principles of object-oriented programming: encapsulation. We will return to this concept a little later.

There are two levels of access control:

  • At the top level (Class)
  • At the member level (Attribute or Method)

4. Access Modifier at Class Level

Only two access modifiers are possible at the class level. These are the “default” and “public” modifiers.

4.1. Access Modifier default

This modifier should not be explicitly specified for a class. A class without an access modifier is automatically assigned this one. Such a class is only visible inside the same package. This modifier is also called “package-private”.

package mypackage;

//This class is only visible in package "mypackage"
class Person{
    private String firstName;
    private String lastName;
}

The code below will not compile

package mypackage;

//The modifier "default" must not be put explicitly
default class Person{ //Compile error here
    private String firstName;
    private String lastName;
}

4.2. Access Modifier public

A class with the “public” modifier is visible to all classes everywhere.

//This class is visible everywhere
public class Person{
    private String firstName;
    private String lastName;
}

Note: You may declare multiple Java classes within the same “.java” file, but at most one of those classes must be “public”. The public class must have the same name as the file.

Assuming that you have a Java file named People.java, the code below will not compile

//The public class must have the same name as the file.
public class Person{//Compile error
    private String firstName;
    private String lastName;
}

5. Access Modifier at Member Level

At the method and attribute level, four access modifiers are possible: default, private, protected, and public.

5.1. Access Modifier default

Similar to a class, this modifier limits the method or attribute visibility to the declaring package. Just like with classes, avoid adding this modifier explicitly; it is automatically considered when a method lacks an access modifier.

package mypackage;

public class Person{
    //These attributes are only visible in the package "mypackage"
    String firstName;
    String lastName;

    //This method is only visible in the package "mypackage"
    void setFirstName(String firstName){
        this.firstName = firstName;
    }
}    

5.2. Access Modifier private

Methods and attributes with this modifier are only visible in the current class.

package mypackage;

public class Person{
    //These attributes are only visible in this class Person
    private String firstName;
    private String lastName;

    //This method is only visible in this class Person
    private void setFirstName(String firstName){
        this.firstName = firstName;
    }
}    

5.3. Access Modifier protected

Methods and attributes with this modifier are only visible in the same package and in subclasses via inheritance.

package mypackage;

public class Person{
    //These attributes are visible to Student and Classroom classes
    protected String firstName;
    protected String lastName;

    //This method is visible to Student and Classroom classes
    protected void setFirstName(String firstName){
        this.firstName = firstName;
    }
}
package mypackage2;

public class Student extends Person{
}
package mypackage;

public class Classroom{
}

“protected” class members of Person.class are visible to both Student and Classroom classes. Why?

  • Student is a subclass of Person, even if they are not in the same package
  • Classroom is not a subclass of Person, but they are in the same package.

5.4. Access Modifier public

Methods and attributes with this modifier are visible everywhere.

package mypackage;

 class Person{
     //These attributes are visible everywhere
    public String firstName;
    public String lastName;

    //This method is visible everywhere
    public void setFirstName(String firstName){
        this.firstName = firstName;
    }
}

The School class below can see all the members of the Person class above, even if they are not in the same class. School is also not a subclass of Person.

package anotherpackage;

class School{
}

6. Conclusion

In this tutorial, you learned the different Access Modifiers in Java: default, private, protected, and public.
You also learned the difference between all these modifiers and how to effectively use each of them.

7. References

1) OCP Oracle Certified Professional Java SE 17 by Khalil A. Mughal and Vasily A. Strelnikov
2) Oracle Java Documentation

Noel Kamphoa

Experienced software engineer with expertise in Telecom, Payroll, and Banking. Now Senior Software Engineer at Societe Generale Paris.

Leave a Reply