access modifiers in Java

This question already has an answer here:

  • In Java, difference between package private, public, protected, and private 26 answers

  • package-private is not a real modifier. You can't type package-private and get the system to recognize it as an access modifier. It's really the default, made by not including any other modifiers.

    It means that the given members can only be accessed in the same package.

    For example, com.hexafraction.Cow can access a member with default modifiers(none actually) in com.hexafraction.Dog , but com.foo.Crow can't access that member as it's not in the same pacakge.

    In this example, the following makes up Cow :

    pacakge com.hexafraction;
    class Cow{
        void moo(){ //no public, protected, or private modifier
            System.out.println("moo!");
        }
    }
    

    Edit for the future: In Java 8, package will supposedly be the modifier needed for this. Literally typing default will still not apply here.


    The so-called "package-private" access level is what occurs without a modifier such as private , protected , or public .

    Example:

    public class Test {
        int test;  // package-private
    }
    

    Anything in the same package, even an unrelated class, can access it, but other classes (even subclasses of the class) outside of the package cannot access it.

    This link to the Java tutorial on the subject should help.

    链接地址: http://www.djcxy.com/p/24064.html

    上一篇: 公共与受保护的抽象类方法

    下一篇: Java中的访问修饰符