Java Access Modifiers and Specifiers

In Java, the access to classes, constructors, methods and fields are regulated using access modifiers i.e. a class can control what information or data can be accessible by other classes. To take advantage of encapsulation, we should minimize access whenever possible.

Actually there are three access modifiers in Java, namely public, private and protected. Java uses these access modifiers to help us set the level of access we want for classes as well as the fields, methods and constructors in our classes. A member has also package or default accessibility when no accessibility modifier is specified. So, there are four access specifiers in Java and they are namely –

1. public
2. private
3. protected
4. default

They are described below one-by-one.

1. public access specifier :

Fields, methods and constructors declared public (least restrictive) within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package.

2. private access specifier :

The private (most restrictive) fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods or constructors declared private are strictly controlled, which means they cannot be accesses by anywhere outside the enclosing class. A standard design strategy is to make all fields private and provide public getter methods for them.

3. protected access specifier :

The protected fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods and constructors declared protected in a superclass can be accessed only by subclasses in other packages. Classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member’s class.

4. default access specifier :

Java provides a default specifier which is used when no access modifier is present. Any class, field, method or constructor that has no declared access modifier is accessible only by classes in the same package. The default modifier is not used for fields and methods within an interface.

See the table below to show which elements of a class are accessible to whom based on the rules imposed by the access specifiers.

Table 1: Visibility Specifiers

NOTE: See this topic for better understanding of Java access specifiers using packages.

 

Java Non-Access Modifiers

There are some non-access modifiers in Java. They are namely, native, final, abstract, static, volatile, transient, strictfp, synchronized etc. We can modify a class declaration using the keyword final, abstract, or strictfp. These modifiers are in addition to whatever access control is on the class, so we could; for example, declaring a class as both public and final. But we can’t always mix non-access modifiers. We are free to use strictfp in combination with final, for example, but we must never, ever, mark a class as both final and abstract.