Access Specifiers Modifiers Java

Access Specifiers Modifiers Java


Access Specifiers Modifiers are different. Specifier specifies access and modifier modifies access. A very confusing for a Beginner. Expalained Simple terms.

Java supports both specifiers and modifiers that can dictate the access. An access specifier can be applied to any identifier like variables, methods and classes but modifiers cannot be applied to every identifier, there are limitations. For example, the modifiers applied to variables cannot be used with classes and similarly with methods also. A full discussion of specifiers is given later.

Following list gives the details of access specifiers and modifiers that can be applied to classes, variables and methods.

Class Specifiers & Modifiers

Three modifiers exist that can be applied to classes. Modifier comes just before the class name.

Modifier Keyword

Meaning
abstract

Objects cannot be created
final

cannot be inherited
strictfp

Guarantees the same precision for floating-point values in the class irrespective of the OS on which the program is executed

The two access specifiers a class can accept are public or default. Default means the specifier is not mentioned at all.

Variable Specifiers & Modifiers

Four modifiers and four specifiers exist that can be applied to variables.

Modifier Keyword

Meaning
static

Also known as "class variable". No object is necessary to call
final

cannot be reassigned
transient

Not serialized
volatile

Value is more liable to change

All the four access specifiers, Java supports, can be applied to variables – public, protected, default and private. If the specifier is not mentioned, it takes default access. A local variable must be default only.

Method Specifiers & Modifiers

Six modifiers exist that can be used with methods.

Modifier Keyword

Meaning
final

Subclass cannot override
abstract

No method body exists
native

Java method takes the help of underlying OS
static

Object is not necessary to call
synchronized

Used to lock the source and renders a thread-safe operaton
strictfp

Guarantees the same precision for floating-point values in the class irrespective of the OS on which the program is executed

All the four access specifiers, public, protected, default and private, can be applied to methods. If not mentioned any access specifier, JVM takes default access.

Access restrictions of all four access specifiers are discussed in Access Specifiers – Accessibility Permissions & Restrictions.

You may be interested in the following also.

1. Access Modifiers – Meanings
2. Rules of Access Specifiers in Method Overriding
3. Rules of Exceptions in Method Overriding

Pass your comments of how far this "Access Specifiers Modifiers "is helpful to you with suggestions.

7 thoughts on “Access Specifiers Modifiers Java

  1. Amit

    There in no Specifier in JAVA, i can proof that. Suppose i define top level class as private but it is not allow.

    private class Tea
    {
    public static void main(String[] args)
    {
    System.out.println(“Hello World!”);
    }
    }

    “Modifier Private not allowed here” there is all Modifier in java no such concept like as Access Specifier like as C++;

  2. sravanthi

    Hi, It’s a great job which was done by you by helping people like me.
    I have a doubt, i have seen one video in that they proved there is no word like access specifier in java , every thing will be treated as modifier
    Example: if every if we try to execute below program, this will display an error saying “private modifier is not allowed here”
    private class xyz{
    public static void main(String args[])
    {
    System.out.print(“hi”);
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *