Thursday , July 7 2022
Home / Java / JAVA #19 – Access Modifiers and More on Classes

JAVA #19 – Access Modifiers and More on Classes

Controlling Access to Members of a Class

Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

At the member level, you can also use the public modifier or no modifier ( package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected. Theprivate modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

The following table shows the access to members permitted by each modifier.

Access Levels
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N


package accessmodifiers;
public class AccessModifiers {
    private int id;
    private String name;
    public int age;
    public void Dis()
        System.out.println("Hello World!");
    private void Dis2()
        System.out.println("Hello World Again!");
    public static void main(String[] args) {
       AccessModifiers objAcc = new AccessModifiers();


About Faheem

An adaptable and responsible Software Engineer, who enjoys working with computers and the opportunity to learn new skills and work with new technologies is particularly attractive to me.

Check Also

JAVA #32 – Java application using SQL server (Part: 01)

  In this part we made the GUI front end of a Application.

Leave a Reply

Your email address will not be published.