What is the difference between access specifiers and access modifiers?

拈花ヽ惹草 提交于 2019-12-29 02:54:46

问题


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


回答1:


"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.




回答2:


Referring to the Sun Java Docs they both seem to be the same:

  • Access Modifier
  • Search for access specifier on this page.



回答3:


The term Access specifier used by c++ programmers not in java. In java Officially we use Access Modifier.

For example: when we declare a class with private, static the compiler clearly shows the error message as follows:




回答4:


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.




回答5:


According to me, yes, both terms refer to the same thing and are used interchangeably.




回答6:


That JDI reference is the only place I have ever seen the term 'access specifier' used in a Java specification. Even there, public/protected/private/package are also called 'modifiers'. There's really no reason to ever use the term 'access specifier' in Java, it is clearly just a mistake on one page out of many thousands.




回答7:


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.




回答8:


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.




回答9:


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


来源:https://stackoverflow.com/questions/2238730/what-is-the-difference-between-access-specifiers-and-access-modifiers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!