In Java, the access modifiers give accessibility (scope) of a data member, method, constructor or class. Access modifiers help us to set the level of access for our class, variables, and methods. In Java, two types of modifiers are access modifiers and non-access modifiers. In Java, Access modifiers can be defined as
Keywords that help set the visibility and accessibility
of a class, its member variables, and methods are called Access Modifier.
Access modifiers specify who can access them. There are four access
modifiers used in java. They are public, private, protected, default (declaring without an access modifier). Using no modifier is also sometimes referred to as
"package-private" or "default" or
"friendly" access.
Usage of these access modifiers is restricted to two levels. The two levels are
class-level access modifiers and member level access modifiers:
Class level access modifiers
Only two access modifiers are allowed,
public and no modifier. If a class is 'public', then it can be accessed from
anywhere. If a class has "no modifier", then it can
only be accessed from "same" package'.
Member level access modifiers
All the four public, private, protected
and no modifier is allowed. public and no modifier - the same way as used in
class level. private - members can only access. protected - can be accessed from the same package and a subclass existing in any
package can access.
The Following Table shows what Access Modifiers are appropriate for classes,
nested classes, member variables, interface, and interface methods.
Access Modifier |
Class |
Nested class |
Member variable |
Interface |
Interface method |
public |
visible from anywhere |
same as its class |
same as its class |
visible from anywhere |
visible from anywhere |
protected |
N/A |
it's class and it's a subclass |
it is class and it's a subclass |
N/A |
N/A |
package |
only from its a package |
only From its the package |
only from its package |
N/A |
N/A, the default package |
default |
N/A |
only from its class |
only from its class |
N/A |
N/A |
Public Keyword
The public is a Java keyword which declares a member's access as public. Public
members are visible to all other classes. This means that any other class can
access a public field or method. Further, other classes can modify public fields
unless the field is declared as final. A best practice is to give fields private
access and reserve public access to only the set of methods and final fields
that define the class' public constants. This helps with encapsulation and
information hiding since it allows you to change the implementation of a class
without affecting the consumers who use only the public API of the class.
Protected Keyword
Protected is a Java keyword. This keyword is an access modifier, used before a method or other class member to signify that the method or variable can only be
accessed by elements residing in its class, subclasses, or classes in the same
package.
Syntax:
protected <return Type> <method Name>(<parameters>);
For example:
protected int getAge();
protected void setYearOfBirth(int year);
Private Keyword
Private is a Java keyword which declares a member's
access as private. That is, the member is only visible within the class, not
from any class. The visibility of private members extends to nested classes.
Syntax:
private void method();
Default Access Modifier - No keyword
Default access modifier means we do not
explicitly declare an access modifier for a class, field, method, etc. A variable
or method declared without any access control modifier is available to any other
class in the same package. The default modifier cannot be used for methods,
fields in an interface.
Example:
Variables and methods can be declared without any modifiers, as in the following
examples:
Some Example Of Uses Of these Access Specifier.
A.java:
- class Exp
- {
- private String name;
- protected void set(String nm)
- {
- name = nm;
- }
- public Exp(String name)
- {
- this.name = name;
- }
- public String toString()
- {
- return "I'm a vikas and my name is " + name;
- }
- }
- public class A extends Exp
- {
- private int aNumber;
- public A(String name, int aNumber)
- {
- super(name);
- this.aNumber = aNumber;
- }
- public void change(String name, int aNumber)
- {
- set(name);
- this.aNumber = aNumber;
- }
- public String toString()
- {
- return "A" + aNumber + ": " + super.toString();
- }
- public static void main(String[] args)
- {
- A a = new A("vikky", 12);
- System.out.println(a);
- a.change("mishra", 19);
- System.out.println(a);
- }
- }
Output: A.java:
My1.java:
- class MySingleton
- {
- private static MySingleton theObject;
- private MySingleton() {
- }
- public static MySingleton createMySingleton()
- {
- if (theObject == null)
- theObject = new MySingleton();
- return theObject;
- }
- }
- public class My1
- {
- public static void main(String[] args)
- {
- MySingleton ms1 = MySingleton.createMySingleton();
- MySingleton ms2 = MySingleton.createMySingleton();
- System.out.println(ms1 == ms2);
- }
- }
Output:
Summary
In this article, we learned about Access Modifiers in Java and how to use these access modifiers in Java Programs.