Java 中默认的访问说明符是什么?
我刚刚开始阅读一本 Java 书籍并想知道;如果未指定,哪一个访问说明符是默认访问说明符?
I just started reading a Java book and wondered; which access specifier is the default one, if none is specified?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
默认的访问说明符是package。类可以访问同一包中其他类的成员。但在包之外它显示为private
the default access specifier is package.Classes can access the members of other classes in the same package.but outside the package it appears as private
以下是对 Java 创始人 James Gosling 的采访中关于包级可见性的引用:
http://www.artima.com/intv/gosling2P.html
Here is a quote about package level visibility from an interview with James Gosling, the creator of Java:
http://www.artima.com/intv/gosling2P.html
更新 Java 8
default
关键字的用法:正如许多其他人所指出的默认可见性(无关键字)
不要与新的 Java 8 功能相混淆 (默认方法),允许接口在使用
default
关键字标记时提供实现。请参阅:访问修饰符
Update Java 8 usage of
default
keyword:As many others have noted The default visibility (no keyword)
Not to be confused with the new Java 8 feature (Default Methods) that allows an interface to provide an implementation when its labeled with the
default
keyword.See: Access modifiers
JAVA 中有一个名为“default”的访问修饰符,它允许仅在该包内直接创建该实体的实例。
这是一个有用的链接:
Java 访问修饰符/说明符
There is an access modifier called "default" in JAVA, which allows direct instance creation of that entity only within that package.
Here is a useful link:
Java Access Modifiers/Specifiers
首先我要说一件事,java中没有“访问说明符”这样的术语。我们应该将一切称为“修饰符”。我们知道final、static、synchronized、volatile……都被称为修饰符,甚至Public、private、protected、default、abstract也应该被称为修饰符。默认是这样的修饰符,其中物理存在不存在但没有放置修饰符,那么它应该被视为默认修饰符。
为了证明这一点,举一个例子:
输出将是:
Hello Java
现在将 public 更改为 private,看看会出现什么编译器错误:
它说“这里不允许修饰符私有”
结论是某人可能是错的,或者某些教程可能是错的,但编译器不会错。
所以我们可以说java中没有术语访问说明符,一切都是修饰符。
First of all let me say one thing there is no such term as "Access specifier" in java. We should call everything as "Modifiers". As we know that final, static, synchronised, volatile.... are called as modifiers, even Public, private, protected, default, abstract should also be called as modifiers . Default is such a modifiers where physical existence is not there but no modifiers is placed then it should be treated as default modifiers.
To justify this take one example:
Output will be:
Hello Java
Now change public to private and see what compiler error you get:
It says "Modifier private is not allowed here"
What conclusion is someone can be wrong or some tutorial can be wrong but compiler cannot be wrong.
So we can say there is no term access specifier in java everything is modifiers.
默认可见性称为“包私有”(尽管您不能显式使用它),这意味着可以从该类所属的同一包内部访问该字段。
正如 mdma 指出的那样,但对于接口成员来说并非如此,默认值为“public”。
请参阅 Java 的访问说明符
The default visibility is known as “package-private” (though you can't use this explicitly), which means the field will be accessible from inside the same package to which the class belongs.
As mdma pointed out, it isn't true for interface members though, for which the default is "public".
See Java's Access Specifiers
默认说明符取决于上下文。
对于类和接口声明,默认为包私有。这介于受保护和私有之间,仅允许同一包中的类访问。 (protected就是这样,但也允许访问包外的子类。)
对于接口成员(字段和方法),默认访问是public。但请注意,接口声明本身默认为包私有。
如果我们使用 MyInterface2 声明
类,则可以从超级接口中看到 field1 和 method1,因为它们是公共的,即使它们看不到 MyInterface 本身的声明。
The default specifier depends upon context.
For classes, and interface declarations, the default is package private. This falls between protected and private, allowing only classes in the same package access. (protected is like this, but also allowing access to subclasses outside of the package.)
For interface members (fields and methods), the default access is public. But note that the interface declaration itself defaults to package private.
If we then have the declaration
Classes using MyInterface2 can then see field1 and method1 from the super interface, because they are public, even though they cannot see the declaration of MyInterface itself.
如果没有给出访问说明符,则它是类和类成员的包级访问(没有明确的说明符)。接口方法是隐式公共的。
If no access specifier is given, it's package-level access (there is no explicit specifier for this) for classes and class members. Interface methods are implicitly public.
默认可见性(无关键字)是package,这意味着它将可供位于同一包中的每个类使用。
有趣的是,protected 不会限制子类的可见性,也不会限制同一包中其他类的可见性
The default visibility (no keyword) is package which means that it will be available to every class that is located in the same package.
Interesting side note is that protected doesn't limit visibility to the subclasses but also to the other classes in the same package
这取决于事情是什么。
顶级类型(即类、枚举、接口和未在其他类型中声明的注释类型)默认为包私有。 (JLS §6.6.1< /a>)
在类中,所有成员(即字段、方法和嵌套类型声明)和构造函数默认都是包私有。 (JLS §6.6.1< /a>)
在枚举中,构造函数默认是私有。事实上,枚举构造函数必须是私有的,将它们指定为公共或受保护是错误的。枚举常量始终是公共,并且不允许任何访问说明符。默认情况下,枚举的其他成员是包私有。 (JLS §8.9)
在接口和注释类型中,所有成员(同样,这意味着字段、方法和嵌套类型声明)默认都是public。事实上,接口和注释类型的成员必须是公共的,将它们指定为私有或受保护是错误的。 (JLS §9.3 至 9.5)
本地类是在方法、构造函数或初始化块内声明的命名类。它们作用域为声明它们的
{
..}
块,并且不允许任何访问说明符。 (JLS §14.3)使用反射,您可以从其他地方实例化本地类,并且它们是包私有的,尽管我不确定该细节是否在 JLS 中。匿名类是使用
new
创建的自定义类,它直接在表达式中指定类主体。 (JLS §15.9.5< /a>) 它们的语法不允许任何访问说明符。使用反射,您可以从其他地方实例化匿名类,它们及其生成的构造函数都是包私有的,尽管我不确定该细节是否在 JLS 中。实例和静态初始化块在语言级别没有访问说明符 (JLS §8.6 和 8.7),但静态初始化块是作为名为
的方法实现的(JVMS §2.9),因此该方法必须在内部,有一些访问说明符。我使用十六进制编辑器检查了由 javac 和 Eclipse 编译器编译的类,发现两者都将方法生成为 package-private。但是,您无法在该语言中调用()
,因为<
和>
字符在方法中无效名称,并且反射方法被硬连线以否认其存在,因此实际上其访问说明符是无访问。该方法只能由 VM 在类初始化期间调用。 实例初始化块不会被编译为单独的方法;它们的代码被复制到每个构造函数中,因此即使通过反射也无法单独访问它们。It depends on what the thing is.
Top-level types (that is, classes, enums, interfaces, and annotation types not declared inside another type) are package-private by default. (JLS §6.6.1)
In classes, all members (that means fields, methods, and nested type declarations) and constructors are package-private by default. (JLS §6.6.1)
In enums, constructors are private by default. Indeed, enum contructors must be private, and it is an error to specify them as public or protected. Enum constants are always public, and do not permit any access specifier. Other members of enums are package-private by default. (JLS §8.9)
In interfaces and annotation types, all members (again, that means fields, methods, and nested type declarations) are public by default. Indeed, members of interfaces and annotation types must be public, and it is an error to specify them as private or protected. (JLS §9.3 to 9.5)
Local classes are named classes declared inside a method, constructor, or initializer block. They are scoped to the
{
..}
block in which they are declared and do not permit any access specifier. (JLS §14.3) Using reflection, you can instantiate local classes from elsewhere, and they are package-private, although I'm not sure if that detail is in the JLS.Anonymous classes are custom classes created with
new
which specify a class body directly in the expression. (JLS §15.9.5) Their syntax does not permit any access specifier. Using reflection, you can instantiate anonymous classes from elsewhere, and both they and their generated constructors are are package-private, although I'm not sure if that detail is in the JLS.Instance and static initializer blocks do not have access specifiers at the language level (JLS §8.6 & 8.7), but static initializer blocks are implemented as a method named
<clinit>
(JVMS §2.9), so the method must, internally, have some access specifier. I examined classes compiled by javac and by Eclipse's compiler using a hex editor and found that both generate the method as package-private. However, you can't call<clinit>()
within the language because the<
and>
characters are invalid in a method name, and the reflection methods are hardwired to deny its existence, so effectively its access specifier is no access. The method can only be called by the VM, during class initialization. Instance initializer blocks are not compiled as separate methods; their code is copied into each constructor, so they can't be accessed individually, even by reflection.default 是用作方法和变量的访问修饰符的关键字。
使用此访问修饰符将使您的类、变量、方法或构造函数可以从自己的类或包访问,如果不存在访问修饰符,它也将被设置。
如果您在接口中使用默认值,您将能够在那里实现一个方法,如下例所示,
但是它仅适用于 Java 8 版本
官方文档
访问修饰符Java
default is a keyword that is used as an access modifier for methods and variables.
Using this access modifier will make your class, variable, method or constructor acessible from own class or package, it will be also is set if no access modifier is present.
if you use a default in a interface you will be able to implement a method there like this exemple
However it will only works from the 8 Java version
Official Documentation
Access Modifiers in Java
有关更多详细信息,请参阅此处。默认不是私有/公共/受保护的,而是完全不同的访问规范。它没有被广泛使用,我更喜欢在我的访问定义中更加具体。
See here for more details. The default is none of private/public/protected, but a completely different access specification. It's not widely used, and I prefer to be much more specific in my access definitions.