Java Modifiers With Examples

Java Modifiers With Examples

Modifiers in Java :

Java provides access specifiers to allow the library creator to say what is available to the client programmer and what is not. The levels of access control from “most access” to “least access” are public, protected, package access (which has no keyword), and private. From the previous paragraph you might think that, as a library designer, you’ll want to keep everything as “private” as possible, and expose only the methods that you want the client programmer to use. This is exactly right, even though it’s often counterintuitive for people who program in other languages (especially C) and who are used to accessing everything without restriction.

There are two types of modifiers in java we will discuss them as follows -

Types of Modifiers-

  • Non-Access modifiers
  • Access Modifiers

They do not control the accessibility of member from outside the boundary of a class.

Abstract, static, final …… are the examples of non-access modifiers.

Access Modifiers

They control the accessibility of class members outside the boundary of a class.

Java supports the following access modifiers.

Types of access modifiers

  • Public
  • Private
  • Protected
  • Default ( Friendly ) [ when we not mention any modifier ]


Example2 -

Example 3 -

public class T{
int i =1;
static void show() {

public static void main(String[] args) {
Output -



The private keyword means that no one can access that member except the class that contains that member, inside methods of that class. Other classes in the same package cannot access private members, so it’s as if you’re even insulating the class against yourself. On the other hand, it’s not unlikely that a package might be created by several people collaborating together, so private allows you to freely change that member without concern that it will affect another class in the same package.

Note - An outer class can not be private or protected in java. But inner class can be private, and protected.