Java 访问说明符和访问修饰符有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2238730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:06:48  来源:igfitidea点击:

What is the difference between access specifiers and access modifiers?

javaaccess-modifiersaccess-specifier

提问by sgokhales

In Java, are access specifiersand access modifiersthe same thing?

在 Java 中,访问说明符访问修饰符是一回事吗?

采纳答案by Michael Borgwardt

"access modifier" is the official term for private, protectedand publicused 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.

“访问修饰符”是官方用语privateprotectedpublic在使用Java语言规范。“访问说明符”在Java API 文档中同义使用,但这是我第一次注意到这一点。坚持使用 JLS 术语可能更好。

回答by missingfaktor

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

在我看来,是的,这两个术语指的是同一件事,并且可以互换使用。

回答by questzen

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.

从技术上讲,private、public、protected 和 default 被视为访问说明符。这些处理谁可以......问题。修饰符 afaik 是 volatile、final、static、transient 等。这些处理如何……方面。

回答by codaddict

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

参考 Sun Java Docs 他们似乎是一样的:

回答by user207421

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.

该 JDI 参考是我在 Java 规范中唯一见过术语“访问说明符”的地方。即使在那里,public/protected/private/package 也被称为“修饰符”。在 Java 中真的没有理由使用术语“访问说明符”,这显然只是成千上万页中的一个错误。

回答by Tamana

Java has basically 2 types of Modifiers:

Java 基本上有两种类型的修饰符:

  1. java access modifiers
  2. java non-access modifiers
  1. java访问修饰符
  2. java非访问修饰符

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

Java 访问修饰符和 Java 访问说明符是一回事,它们是public, private, protected

回答by sajid khan

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 -

通过使用访问说明符,我们定义了谁可以访问我们的类/方法和变量(或我们使用访问说明符的任何内容)。基本上java访问说明符有四种类型 -

  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
  1. 公开:- 对世界可见,
  2. 私人:- 仅对班级可见,
  3. 受保护:- 对包和所有子类可见,以及
  4. 默认值:- 对包可见

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
  1. final:- 用于完成类、方法和变量的实现
  2. static:- 用于创建类方法和变量
  3. 同步和 volatile 修饰符:- 用于线程
  4. 抽象:-用于创建抽象类和方法
  5. 短暂的

回答by raghavanm

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.

在一些较旧的语言中,public、private、protected 和 default(如 C++)被视为访问说明符,而其他一切都被视为访问修饰符,但在 Java 中,说明符没有术语,默认情况下,一切都仅被视为修饰符。所以 public、private、protected、default、final、abstract、static、strictfp、synchronized、native、transient 和 volatile 都只是修饰符。

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.

我们将收到编译时错误,指出此处不允许使用修饰符 private。对于其他修饰符也是如此。也许 java 编译器 (javac) 仅将所有内容视为“修饰符”。

回答by Premraj

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

c++ 程序员使用的术语访问说明符不在 java 中。在 java 中,我们正式使用Access Modifier

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

例如:当我们用private、static声明一个类时,编译器清楚地显示错误信息如下:
在此处输入图片说明