What is the difference between access specifiers and access modifiers?

后端 未结 10 735
我在风中等你
我在风中等你 2020-12-13 02:02

In Java, are access specifiers and access modifiers the same thing?

相关标签:
10条回答
  • 2020-12-13 02:21

    Java has basically 2 types of Modifiers:

    1. java access modifiers
    2. java non-access modifiers

    Java access modifiers and Java access specifiers are the same thing, which are public, private, protected.

    0 讨论(0)
  • 2020-12-13 02:21

    There is nothing known as "Access specifiers" in java, there are only Access modifiers in java

    The misconception is from languages like C++ where public, private, protected, default are considered as Access specifiers and remaining (static, final, etc) are considered as access modifiers

    Following is the proof as compiler says "modifier private not allowed here" i.e. compiler said modifier and not specifier

    0 讨论(0)
  • 2020-12-13 02:28

    By using access specifier we define that who one can access our class/method and variable(or whatever with that we use access specifier ). basically java access specifier are four types -

    1. public:- Visible to the world,
    2. private:- Visible to the class only,
    3. protected:- Visible to the package and all subclasses, and
    4. default:- Visible to the package

    But access modifier are properties of a class/method/variable. Access modifier are five types

    1. final:- for finalizing the implementations of classes, methods, and variables
    2. static:- for creating class methods and variables
    3. Synchronization and volatile modifiers:- which are used for threads
    4. abstract:- for creating abstract classes and methods
    5. transient
    0 讨论(0)
  • 2020-12-13 02:30

    "access modifier" is the official term for private, protected and public used in the Java language specification. "access specifier" is used synonymously in the Java API doc, but this is the first time I've noticed that. It's probably better to stick with the JLS term.

    0 讨论(0)
  • 2020-12-13 02:33

    Technically speaking private, public, protected and default are treated as access specifiers. These deal with who can ... questions. The modifiers afaik are volatile, final, static, transient etc. These deal with how does .. aspect.

    0 讨论(0)
  • 2020-12-13 02:34

    In some older languages public, private, protected and default like C++ are considered as access specifiers and everything else is considered as access modifier but in Java there is no terminology for specifier, everything is by default considered as modifier only. So public, private, protected, default, final, abstract, static, strictfp, synchronized, native, transient and volatile are all modifiers only.

    Simple test for it is when we compile the following code

    private class Test{ }

    we will get compile time error saying that modifier private not allowed here. This is true for other modifiers also. Maybe java compiler (javac) sees everything as a "modifier" only.

    0 讨论(0)
提交回复
热议问题