Access Modifiers in Java (Access Specifiers)

Access modifiers in Java define the visibility and scope of a class, constructor, method, or field. By applying access modifiers in Java, you control how far those fields, constructors, methods, and classes can be reached. They are also called access specifiers in Java. This post walks through every modifier in Java and shows access specifiers in Java with examples.

Below is the table of contents for the topics covered in this article.
1. Default access modifier
i) default class
ii) default variable
iii) default method
iv) default constructor
2. Private access modifier
i) private class
ii) private variable
iii) private method
iv) private constructor
3. Protected access modifier

i) protected class
ii) protected variable
iii) protected method
iv) protected constructor
4. Public access modifier
i) public variable
ii) public method
iii) public constrcutor
iv) public class

Video available in Hindi

Java provides four access modifiers

1. default
2. private

3. protected
4. Public

1. The default access specifier in Java

When you leave out any modifier, Java supplies one automatically, and that is known as the default access modifier.

  • The default access specifier in Java can be reached only inside the package; it is not accessible from outside that package.
  • It is more permissive than the private access modifier, meaning its scope is wider than private.
  • It is more restrictive than the protected access modifier and the public access modifier.

default class

When a programmer adds no modifier to a class, that class is a default class. It can be used anywhere inside the package where it is declared, but it cannot be reached from outside that package.

package packageOne;
class ClassOne 
{
	void display()
	{
		System.out.println("This class have default access modifier");
	}
}
package packageTwo;
import packageOne.ClassOne;
public class ClassTwo 
{
	public static void main(String[] args) 
	{
          System.out.println("We can't import the ClassOne because it not visible outside 
          the package");
          System.out.println("This class will throw error at compilation time");
	}
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem: at packageTwo.ClassTwo.main(ClassTwo.java:5)

default variable

If no modifier is given to a variable, it becomes a default variable. Such a variable is reachable anywhere inside the package where it is declared, but not from outside that package.

package packageOne;
public class ClassOne 
{
	int a = 5;
	String b = "Hi";
}
package packageTwo;
import packageOne.ClassOne;
public class ClassTwo {
	public static void main(String[] args) 
	{
		System.out.println("We can access the ClassOne because it is public");
		ClassOne obj = new ClassOne();
		System.out.println(obj.a);
		System.out.println(obj.b);
	}
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problems: The field ClassOne.a is not visible The field ClassOne.b is not visible at packageTwo.ClassTwo.main(ClassTwo.java:10)

default method

When no modifier is applied to a method, it is a default method. A default method can be called anywhere inside the package where it is declared, but not from outside that package.

package packageOne;
public class ClassOne 
{
	void display()
	{
		System.out.println("This is default method of class");
	}
}
package packageTwo;
import packageOne.ClassOne;
public class ClassTwo
{
	public static void main(String[] args) 
	{
		System.out.println("We can access the ClassOne because it is public");
		ClassOne obj = new ClassOne();
		obj.display();
	}
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem: The method display() from the type ClassOne is not visible at packageTwo.ClassTwo.main(ClassTwo.java:10)

default constructor

As you already know, a class can have a constructor. If no modifier is supplied when the constructor is declared, it is a default constructor. It can be called inside the package where it is declared, but not from outside that package, so you cannot create an object of the class from outside the package.

package packageOne;
public class ClassOne 
{
	ClassOne()
	{
		System.out.println("ok");
	}
}
package packageTwo;
import packageOne.ClassOne;
public class ClassTwo
{
	public static void main(String[] args) 
	{
		System.out.println("We can access the ClassOne because it is public");
		ClassOne obj = new ClassOne();
	}
}

NOTE: We can’t create objects outside the package by use of the default constructor of the class whether the class is public or not.

2. The private access specifier in Java

The private access specifier in Java is written with the private keyword. A private access modifier limits scope to the class itself.

  1. Private variables and methods can be used only inside the class.
  2. It is the most restrictive access modifier in Java.

private class

A top-level class cannot be declared private. For top-level classes, Java permits only the public and default modifier. Inner classes, however, can be private; you can read more about them in inner classes.

private variables

You declare private variables with the private keyword. A private variable cannot be reached from outside its class. To block other classes from accessing a variable, apply the private keyword to that variable. If another class tries to use it, the compiler reports an error at compilation time.

public class ClassOne 
{
	private int a = 5;
	private String b = "Hello";
}
public class ClassTwo
{
	public static void main(String[] args) 
	{
		ClassOne obj = new ClassOne();
		System.out.println(obj.a);
		System.out.println(obj.b);
	}
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problems: The field ClassOne.a is not visible The field ClassOne.b is not visible at ClassTwo.main(ClassTwo.java:6)

private method

You declare a private method with the private keyword. A private method cannot be reached from outside its class. To block other classes from calling a method, apply the private keyword to that method. If another class tries to call it, the compiler reports an error at compilation time.

Real-life example: Suppose you need a method that sets a user's password, but you want to restrict it so outsiders cannot change that password. In that case, you can apply the private keyword to the method.

public class ClassOne 
{
	private String password;
	private void setPassword(String password)
	{
		this.password = password;
	}
}
public class ClassTwo
{
	public static void main(String[] args) 
	{
		ClassOne obj = new ClassOne();
		obj.setPassword("NewOne");
	}
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem: The method setPassword(String) from the type ClassOne is not visible at ClassTwo.main(ClassTwo.java:6)

private constructor

You declare a private constructor with the private keyword. A private constructor cannot be reached from outside its class. To block other classes from using a constructor, apply the private keyword to that constructor. If another class tries to use it, the compiler reports an error at compilation time.

public class ClassOne 
{
	private ClassOne()
	{
		System.out.println("Private constrcutor");
	}
}
public class ClassTwo
{
	public static void main(String[] args) 
	{
		ClassOne obj = new ClassOne();
	}
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem:  The constructor ClassOne() is not visible at ClassTwo.main(ClassTwo.java:5)

3. The protected access specifier in Java

The protected access specifier in Java is written with the protected keyword. A protected access modifier reaches inside the package and also outside it, but only through inheritance.

  1. The protected keyword cannot be used with a class.
  2. It is more permissive than the default and private modifiers.
  3. To use a protected variable, method, or constructor outside the package, you must rely on inheritance.

protected class

Just like a private class, a top-level class cannot be declared protected. For top-level classes, Java permits only the public and default modifier. You can read more about this in inner classes.

protected variables

You declare protected variables with the protected keyword. In Java, a protected variable can be reached inside the package and outside it, but only through inheritance. If another class outside the package tries to use these variables, the compiler reports an error at compilation time.

package packageOne;
public class ClassOne 
{
	protected int a = 5;
	protected String b = "Hello";
}
package packageTwo;
import packageOne.ClassOne;
public class ClassTwo extends ClassOne
{
	public static void main(String[] args) 
	{
		ClassTwo obj = new ClassTwo();
		System.out.println(obj.a);
		System.out.println(obj.b);
	}
}

Output: 5
Hello

protected Method

You declare a protected method with the protected keyword. In Java, a protected method can be called inside the package and outside it, but only through inheritance. If another class outside the package tries to call these methods, the compiler reports an error at compilation time.

package packageOne;
public class ClassOne 
{
	protected void display()
	{
		System.out.println("Protected method of ClassOne");
	}
}
package packageTwo;
import packageOne.ClassOne;
public class ClassTwo extends ClassOne
{
	public static void main(String[] args) 
	{
		ClassTwo obj = new ClassTwo();
		obj.display();
	}
}

Output: Protected method of ClassOne

protected constructor

You declare a protected constructor with the protected keyword. A protected constructor can be used inside the package. If another class outside the package tries to use the protected constructor, the compiler reports an error at compilation time.

public class ClassOne 
{
	protected ClassOne()
	{
		System.out.println("Private constrcutor");
	}
}
public class ClassTwo
{
	public static void main(String[] args) 
	{
		ClassOne obj = new ClassOne();
	}
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem: The constructor ClassOne() is not visible at ClassTwo.main(ClassTwo.java:5)

4. The public access specifier in Java

The public access modifier in Java is written with the public keyword. A public access modifier has scope everywhere.

  1. It is the most permissive modifier in Java.
  2. It imposes no restrictions.

public variables

You declare public variables with the public keyword. A public variable can be reached anywhere in the application and from any package.

package packageOne;
public class ClassOne 
{
	public int a = 5;
	public String b = "Hello";
}
package packageTwo;
import packageOne.ClassOne;
public class ClassTwo 
{
	public static void main(String[] args) 
	{
		ClassTwo obj = new ClassTwo();
		System.out.println(obj.a);
		System.out.println(obj.b);
	}
}

Output: 5
Hello

public Method

You declare a public method with the public keyword. A public method can be called from anywhere in your project.

package packageOne;
public class ClassOne 
{
	public void display()
	{
		System.out.println("Public method of ClassOne");
	}
}
package packageTwo;
import packageOne.ClassOne;
public class ClassTwo 
{
	public static void main(String[] args) 
	{
		ClassTwo obj = new ClassTwo();
		obj.display();
	}
}

Output: Public method of ClassOne

public constructor

You declare a public constructor with the public keyword. A public constructor can be used anywhere in the application.

public class ClassOne 
{
	public ClassOne()
	{
		System.out.println("Public constructor");
	}
}
public class ClassTwo
{
	public static void main(String[] args) 
	{
		ClassOne obj = new ClassOne();
	}
}

Output: Public constructor

public class

You can refer to the examples above, since they already use a public class.

Access modifiers in Java

2 thoughts on “Access modifiers in Java”

  1. is it right to put in the main method

    public static void main (String[] args) <—??

    Reply
  2. hello
    thanks you much for this article
    I saw in the example on the protected that you used the private constructor Instead of protected constructor
    please edit the example in that point and put protected constructor
    thanks you .

    Reply

Leave a Comment

Choosing the right access modifier comes down to how widely a member should be visible. Keep fields private unless another class truly needs them, use default or protected when package-level or inherited access is enough, and reserve public for the API you intend others to call. Reviewing each class, variable, method, and constructor against these four levels keeps your Java code encapsulated and easier to maintain.