Access Modifiers in Java – Java中的访问修改器

最后修改: 2018年 5月 20日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’re going over access modifiers in Java, which are used for setting the access level to classes, variables, methods, and constructors.

在本教程中,我们将讨论Java中的访问修饰符,它用于设置对类、变量、方法和构造函数的访问级别。

Simply put, there are four access modifiers: public, private, protected and default (no keyword).

简单地说,有四个访问修改器:publicprivateprotecteddefault(没有关键字)。

Before we begin let’s note that a top-level class can use public or default access modifiers only. At the member level, we can use all four.

在我们开始之前,让我们注意到顶层类只能使用publicdefault访问修改器。在成员层,我们可以使用所有四个。

2. Default

2.默认

When we don’t use any keyword explicitly, Java will set a default access to a given class, method or property. The default access modifier is also called package-private, which means that all members are visible within the same package but aren’t accessible from other packages:

当我们没有明确使用任何关键字时,Java将为一个给定的类、方法或属性设置一个默认访问。默认访问修改器也被称为包-私有,这意味着所有成员在同一包内是可见的,但不能从其他包中访问。

package com.baeldung.accessmodifiers;

public class SuperPublic {
    static void defaultMethod() {
        ...
    }
}

defaultMethod() is accessible in another class of the same package:

defaultMethod()可以在同一个包的另一个类中访问。

package com.baeldung.accessmodifiers;

public class Public {
    public Public() {
        SuperPublic.defaultMethod(); // Available in the same package.
    }
}

However, it’s not available in other packages.

然而,它在其他软件包中是不可用的。

3. Public

3.公共

If we add the public keyword to a class, method or property then we’re making it available to the whole world, i.e. all other classes in all packages will be able to use it. This is the least restrictive access modifier:

如果我们在一个类、方法或属性上添加public关键字,那么我们让它对整个世界可用,也就是说,所有包中的所有其他类都能使用它。这是限制性最小的访问修改器。

package com.baeldung.accessmodifiers;

public class SuperPublic {
    public static void publicMethod() {
        ...
    }
}

publicMethod() is available in another package:

publicMethod()在另一个包中可用。

package com.baeldung.accessmodifiers.another;

import com.baeldung.accessmodifiers.SuperPublic;

public class AnotherPublic {
    public AnotherPublic() {
        SuperPublic.publicMethod(); // Available everywhere. Let's note different package.
    }
}

For more details on how the public keyword behaves when applied to a class, interface, nested public class or interface and method, see the dedicated article.

关于public关键字在应用于类、接口、嵌套的公有类或接口和方法时的行为的更多细节,请参阅专用文章

4. Private

4.私人

Any method, property or constructor with the private keyword is accessible from the same class only. This is the most restrictive access modifier and is core to the concept of encapsulation. All data will be hidden from the outside world:

任何带有private关键字的方法、属性或构造函数都只能从同一个类中访问。这是限制性最强的访问修改器,是封装概念的核心。所有的数据都将从外界隐藏起来。

package com.baeldung.accessmodifiers;

public class SuperPublic {
    static private void privateMethod() {
        ...
    }
    
     private void anotherPrivateMethod() {
         privateMethod(); // available in the same class only.
    }
}

This more detailed article will show how the private keyword behaves when applied to a field, constructor, method and to an inner class.

这篇更加详细的文章将展示private关键字在应用于字段、构造函数、方法和内部类时的表现。

5. Protected

5 受保护

Between public and private access levels, there’s the protected access modifier.

publicprivate访问级别之间,有一个protected访问修改器。

If we declare a method, property or constructor with the protected keyword, we can access the member from the same package (as with package-private access level) and in addition from all subclasses of its class, even if they lie in other packages:

如果我们用protected关键字声明一个方法、属性或构造函数,我们可以从相同的包(与package-private访问级别一样)以及其类的所有子类中访问该成员,即使它们位于其他包中。

package com.baeldung.accessmodifiers;

public class SuperPublic {
    static protected void protectedMethod() {
        ...
    }
}

protectedMethod() is available in subclasses (regardless of the package):

protectedMethod()在子类中是可用的(不管是什么包)。

package com.baeldung.accessmodifiers.another;

import com.baeldung.accessmodifiers.SuperPublic;

public class AnotherSubClass extends SuperPublic {
    public AnotherSubClass() {
        SuperPublic.protectedMethod(); // Available in subclass. Let's note different package.
    }
}

The dedicated article describes more about the keyword when used in a field, method, constructor, inner class and the accessibility in the same package or a different package.

专门的文章描述了更多关于在同一包或不同包的字段、方法、构造函数、内类和可访问性中使用该关键字的情况。

6. Comparison

6.比较

The table below summarises the available access modifiers. We can see that a class, regardless of the access modifiers used, always has access to its members:

下表总结了可用的访问修改器。我们可以看到,一个类,无论使用何种访问修饰符,总是可以访问其成员。

Modifier Class Package Subclass World
public
Y Y Y Y
protected
Y Y Y N
default
Y Y N N
private
Y N N N

7. Conclusion

7.结论

In this short article, we went over access modifiers in Java.

在这篇短文中,我们介绍了Java中的访问修改器。

It’s good practice to use the most restrictive access level possible for any given member to prevent misuse. We should always use the private access modifier unless there is a good reason not to.

对任何给定的成员使用尽可能多的限制性访问级别以防止滥用是一个好的做法。我们应该始终使用private访问修饰符,除非有充分的理由不这样做。

Public access level should only be used if a member is part of an API.

Public访问级别应该只在成员是API的一部分时使用。

As always, the code examples are available over on Github.

一如既往,代码示例可在Github上获取。