Java Public Private

Summary: in this tutorial, you will learn about Java public and private access modifiers, and how to use them to control the access to members of a class effectively.

Introduction to Java public and private access modifiers

In Java, access modifiers control the visibility and accessibility of class members including fields and methods. They determine which members can be accessed only within the class and/or from the outside of the class.

Java has the following access modifiers:

  • public
  • private
  • protected

This tutorial focuses on the first two access modifiers: public and private.

public access modifier

The public access modifier allows a field or method can be accessed from other classes. To declare a member as public, you use the public keyword:

public class MyClass {
    public int publicField;
    
    public void publicMethod() {
    }
}Code language: Java (java)

For example, the following marks the sayHi() method of the Person class as public:

class Person {
    String name;
    int age;

    public void sayHi() {
        System.out.printf("Hi. It's %s.", this.name);
    }
}Code language: Java (java)

By marking the sayHi() method as public, you can call the sayHi() method from any class.

In practice, you use the public access modifiers when you want a class member to be accessible from any part of your program.

Notice that if a member has no access modifier, it has something called the “default” access modifier. In this case, the member will be accessible within the same package in which it is defined. You’ll learn more about packages in the upcoming tutorial.

private access modifier

The private access modifier restricts access to the members within the same class. It means that private members can be only accessible within the same class. In other words, private members are not visible from other classes

To mark a member as private, you use the private keyword:

public class MyClass {
    private int privateField;
   
    private void privateMethod() {
    }
}Code language: Java (java)

For example, the following marks the name and age of the Person class as private:

class Person {
    private String name;
    private int age;

    public void sayHi() {
        System.out.printf("Hi. It's %s.", this.name);
    }
}Code language: Java (java)

Typically, you use the private access modifier when you want to prevent direct access from other classes and promote encapsulation by hiding the implementation details.

In this example, you don’t want other classes to directly access the name and age fields.

Instead, you will provide public methods, known as getters/setters, so that other classes can call them to manipulate these fields. Within these public methods, you can incorporate logic to handle these fields, such as validating before applying changes.

Java public and private guidelines

Here are the general guidelines for using public and private access modifiers:

  • Use public methods for intended public API. Minimize the use of public fields to avoid direct access and promote encapsulation.
  • Use private methods for helper functions. Favor encapsulation by making fields private.
  • Use the least privilege principle when choosing access modifiers, which expose only what is necessary for other classes to use.

Summary

  • Java access modifiers control access to members of a class including fields and methods.
  • Use the public access modifier when you want the members of a class to be accessible from any other classes.
  • Use the private access modifier when you want the members of a class to be accessible only within the class they defined.
Was this tutorial helpful ?