What is the default access modifier in Java?

This question already has an answer here:

  • Which is the default access specifier in Java? 11 answers

  • From Java documentation

    If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

    At the member level , you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning.

    Full story you can read here (Which I wrote recently):

    http://codeinventions.blogspot.com/2014/09/default-access-modifier-in-java-or-no.html


    从文档:

    Access Levels
    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
    

    It depends on the context.

    When it's within a class:

    class example1 {
    
        int a = 10; // This is package-private (visible within package)
    
        void method1() // This is package-private as well.
        {
            -----
        }
    }
    

    When it's within a interface:

    interface example2 {
    
        int b = 10; // This is public and static.
        void method2(); // This is public and abstract
    }
    
    链接地址: http://www.djcxy.com/p/24046.html

    上一篇: 包冲突情况下的私人访问修改器行为

    下一篇: Java中的默认访问修饰符是什么?