Java 中默认的访问说明符是什么?

发布于 2024-09-15 05:37:55 字数 52 浏览 10 评论 0原文

我刚刚开始阅读一本 Java 书籍并想知道;如果未指定,哪一个访问说明符是默认访问说明符?

I just started reading a Java book and wondered; which access specifier is the default one, if none is specified?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

陌伤ぢ 2024-09-22 05:37:56

默认的访问说明符是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

做个少女永远怀春 2024-09-22 05:37:56

以下是对 Java 创始人 James Gosling 的采访中关于包级可见性的引用:

Bill Venners:Java 有四个访问级别。默认是包。我
一直想知道将包访问设置为默认值是否方便
因为 C++ 人员已经知道的三个关键字
是私有的、受保护的和公共的。或者如果你有一些特别的
您认为包访问应该是默认的原因。

James Gosling:包通常是一组东西,它们是
一起写的。所以一般来说我可以做两件事之一。
其中之一是强迫您始终输入一个关键字,该关键字可以为您提供
领域。或者我可以有一个默认值。然后问题是,
什么是合理的默认值?我倾向于追求最少的东西
危险的事情。

因此,默认设置为 public 确实是一件很糟糕的事情。
私有化可能是一件坏事,如果
只是因为人们实际上并不经常编写私有方法。
受保护也是如此。在查看一堆代码时
我曾经,我决定最常见的东西是相当安全的
在包裹里。 C++ 没有关键字,因为
他们没有包的概念。

但我喜欢它而不是朋友的概念,因为和朋友在一起
你必须列举出你所有的朋友是谁,所以如果
您将一个新类添加到包中,那么您通常最终不得不
去参加该课程包中的所有课程并更新他们的朋友,
我一直觉得这是一件彻头彻尾的痛苦。

但是好友列表本身会导致某种版本控制问题。和
所以就有了“友好班级”的概念。好的事情是
我将其设置为默认值 - 我将解决问题,所以应该做什么
关键字是?

有一段时间实际上有一个友好的关键字。但因为所有的
其他人以“P”开头,而“philendly”则以“PH”开头。但那是
只在那里待一天。

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:

Bill Venners: Java has four access levels. The default is package. I
have always wondered if making package access default was convenient
because the three keywords that people from C++ already knew about
were private, protected, and public. Or if you had some particular
reason that you felt package access should be the default.

James Gosling: A package is generally a set of things that are kind of
written together. So generically I could have done one of two things.
One was force you always to put in a keyword that gives you the
domain. Or I could have had a default value. And then the question is,
what makes a sensible default? And I tend to go for what is the least
dangerous thing.

So public would have been a really bad thing to make the default.
Private would probably have been a bad thing to make a default, if
only because people actually don't write private methods that often.
And same thing with protected. And in looking at a bunch of code that
I had, I decided that the most common thing that was reasonably safe
was in the package. And C++ didn't have a keyword for that, because
they didn't have a notion of packages.

But I liked it rather than the friends notion, because with friends
you kind of have to enumerate who all of your friends are, and so if
you add a new class to a package, then you generally end up having to
go to all of the classes in that package and update their friends,
which I had always found to be a complete pain in the butt.

But the friends list itself causes sort of a versioning problem. And
so there was this notion of a friendly class. And the nice thing that
I was making that the default -- I'll solve the problem so what should
the keyword be?

For a while there actually was a friendly keyword. But because all the
others start with "P," it was "phriendly" with a "PH." But that was
only in there for maybe a day.

http://www.artima.com/intv/gosling2P.html

巴黎盛开的樱花 2024-09-22 05:37:56

更新 Java 8 default 关键字的用法:
正如许多其他人所指出的默认可见性(无关键字)

可以从与该字段相同的包内部访问该字段
班级所属。

不要与新的 Java 8 功能相混淆 (默认方法),允许接口在使用 default 关键字标记时提供实现。

请参阅:访问修饰符

Update Java 8 usage of default keyword:
As many others have noted The default visibility (no keyword)

the field will be accessible from inside the same package to which the
class belongs.

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

复古式 2024-09-22 05:37:56

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

So尛奶瓶 2024-09-22 05:37:56

首先我要说一件事,java中没有“访问说明符”这样的术语。我们应该将一切称为“修饰符”。我们知道final、static、synchronized、volatile……都被称为修饰符,甚至Public、private、protected、default、abstract也应该被称为修饰符。默认是这样的修饰符,其中物理存在不存在但没有放置修饰符,那么它应该被视为默认修饰符。

为了证明这一点,举一个例子:

public class Simple{  
    public static void main(String args[]){  
     System.out.println("Hello Java");  
    }  
}  

输出将是: 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:

public class Simple{  
    public static void main(String args[]){  
     System.out.println("Hello Java");  
    }  
}  

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.

妄断弥空 2024-09-22 05:37:55

默认可见性称为“包私有”(尽管您不能显式使用它),这意味着可以从该类所属的同一包内部访问该字段。

正如 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

扬花落满肩 2024-09-22 05:37:55

默认说明符取决于上下文。

对于类和接口声明,默认为包私有。这介于受保护和私有之间,仅允许同一包中的类访问。 (protected就是这样,但也允许访问包外的子类。)

class MyClass   // package private
{
   int field;    // package private field

   void calc() {  // package private method

   }
}

对于接口成员(字段和方法),默认访问是public。但请注意,接口声明本身默认为包私有。

interface MyInterface  // package private
{
   int field1;         // static final public

   void method1();     // public abstract
}

如果我们使用 MyInterface2 声明

public interface MyInterface2 extends MyInterface
{

}

类,则可以从超级接口中看到 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.)

class MyClass   // package private
{
   int field;    // package private field

   void calc() {  // package private method

   }
}

For interface members (fields and methods), the default access is public. But note that the interface declaration itself defaults to package private.

interface MyInterface  // package private
{
   int field1;         // static final public

   void method1();     // public abstract
}

If we then have the declaration

public interface MyInterface2 extends MyInterface
{

}

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.

燕归巢 2024-09-22 05:37:55

如果没有给出访问说明符,则它是类和类成员的包级访问(没有明确的说明符)。接口方法是隐式公共的。

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.

耀眼的星火 2024-09-22 05:37:55

默认可见性(无关键字)是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

一直在等你来 2024-09-22 05:37:55

这取决于事情是什么。

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)

    • When a class has no explicitly declared constructor, the compiler inserts a default zero-argument constructor which has the same access specifier as the class. (JLS §8.8.9) The default constructor is commonly misstated as always being public, but in rare cases that's not equivalent.
  • 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.

晨与橙与城 2024-09-22 05:37:55

default 是用作方法和变量的访问修饰符的关键字。
使用此访问修饰符将使您的类、变量、方法或构造函数可以从自己的类或包访问,如果不存在访问修饰符,它也将被设置。

  Access Levels
    Modifier    Class   Package Subclass  EveryWhere
    public        Y        Y       Y         Y
    protected     Y        Y       Y         N
    default       Y        Y       N         N
    private       Y        N       N         N

如果您在接口中使用默认值,您将能够在那里实现一个方法,如下例所示,

public interface Computer {    
    default void Start() {
        throw new UnsupportedOperationException("Error");
    }    
}

但是它仅适用于 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.

  Access Levels
    Modifier    Class   Package Subclass  EveryWhere
    public        Y        Y       Y         Y
    protected     Y        Y       Y         N
    default       Y        Y       N         N
    private       Y        N       N         N

if you use a default in a interface you will be able to implement a method there like this exemple

public interface Computer {    
    default void Start() {
        throw new UnsupportedOperationException("Error");
    }    
}

However it will only works from the 8 Java version

Official Documentation

Access Modifiers in Java

奢欲 2024-09-22 05:37:55

有关更多详细信息,请参阅此处。默认不是私有/公共/受保护的,而是完全不同的访问规范。它没有被广泛使用,我更喜欢在我的访问定义中更加具体。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文