The use of visibility modifiers in Java

后端 未结 8 2185
谎友^
谎友^ 2021-01-06 01:32
class Orange{

    Orange(){
    }

}

What is the difference between the usage of the modifier - in this case, package-private - in front of the cl

相关标签:
8条回答
  • 2021-01-06 02:03

    To start with there are 4 access levels created by 3 access modifiers.

    1. public - accessible everywhere
    2. protected - accessible in the same package and in the children
    3. default - accessible only in the same package
    4. private - accessible only in the same class.

    You are correct about - Modifiers at the level of constructors are directly related to the instantiation of the class.

    Modifiers at the level of Class decide the accessibility of the Class.

    0 讨论(0)
  • 2021-01-06 02:07

    Class modifiers work similarly to method modifiers. Public, private, final, abstract, etc. work.

    Public allows the class and its methods to be accessed by classes from any package.

    No modifier only allows classes to be access from it's defined package.

    Private would prevent all access (no point to this if using with a top-level class).

    Abstract classes allow you to create child classes derived from the parent (abstract) class. For example, you can make an Abstract Shape class and have a Rectangle class extend shape, inheriting all its methods, variables, and forcing it to define any abstract methods.

    0 讨论(0)
  • 2021-01-06 02:08

    The use and types of class level modifiers:

    http://javapapers.com/core-java/access-modifiers-in-java-explain/

    The use and types of constructor level modifiers:

    http://www.careercup.com/question?id=296844#commentThread302715

    0 讨论(0)
  • 2021-01-06 02:18

    First, to assuage any fears, the code you've provided is perfectly valid Java syntax.

    In effect, you've created a class that can only be instantiated/used by other classes in the default package. It would also work if you defined it in a package (e.g. package foo;) since only the classes in package foo could see this class).

    Now, to the crux of the question.

    There are different ways to control access to fields and members. and they each do different things.

    • private visibility is the least visible. Only the defining class can access the field.

    • No modifier, or package private is the second least visible. The defining class and all classes within the package may access the field, but subclasses and the rest of the world cannot.

    • protected is the second most visible. Only other classes are prohibited from accessing the field.

    • public is the most visible. Everything can access the field.

    Modifiers at the level of the class get interesting. This comes from the Java Language Specification, §8.1.1:

    The access modifier public (§6.6) pertains only to top level classes (§7.6) and to member classes (§8.5), not to local classes (§14.3) or anonymous classes (§15.9.5).

    The access modifiers protected and private (§6.6) pertain only to member classes within a directly enclosing class or enum declaration (§8.5).

    The modifier static pertains only to member classes (§8.5.1), not to top level or local or anonymous classes.

    It is a compile-time error if the same modifier appears more than once in a class declaration.

    If two or more (distinct) class modifiers appear in a class declaration, then it is customary, though not required, that they appear in the order consistent with that shown above in the production for ClassModifier.

    In general, a class declaration appears something like this:

    ClassDeclaration:
        NormalClassDeclaration
        EnumDeclaration
    
    NormalClassDeclaration:
        ClassModifiers(opt) class Identifier TypeParameters(opt)
                            Super(opt) Interfaces(opt) ClassBody
    

    Anything with (opt) is considered optional.

    So, what does this pare down to?

    • The JLS mandates that a class does not need a [class] modifier.
    • The JLS mandates that, if a [class] modifier is present, then it follows one of these rules:
      • If the modifier is public, then it is only applicable to top level classes and member classes.
      • If the modifier is protected or private, then it is only applicable to member classes within a directly enclosing class or enumeration.
      • The static modifier may appear, but is only applicable to member classes.

    Constructors have a similar rule set.

    ConstructorDeclaration:
        ConstructorModifiers(opt) ConstructorDeclarator
                                    Throws(opt) ConstructorBody
    
    ConstructorDeclarator:
        TypeParameters(opt) SimpleTypeName ( FormalParameterList(opt) )
    

    Again, this breaks down to:

    • The JLS mandates that a constructor does not need a [constructor] modifier.
    • The JLS mandates that a constructor modifier cannot contain abstract, static, final, native, strictfp, or synchronized.
    • The JLS mandates, if no access modifier is specified for the constructor of a normal class, the constructor has default access (§8.8.3, emphasis mine).
    0 讨论(0)
  • 2021-01-06 02:19

    Access Modifiers:

    • Public - {Can access anywhere in the project}
    • Private - {Can access only inside the class}
    • Protected - {Can access within the package and sub classes}
    • Default - {can access within the package}

    Non-Access Modifiers:

    • Static - {for creating class variable and method}
    • Final - {for creating finalized variable and method}
    • Abstract - {for creating abstract method and class}
    • Synchronized - {for threads}

    Some brief discussion on the above modifiers in this link. Refer it for the better understanding.

    0 讨论(0)
  • 2021-01-06 02:21

    Modifier of class defines who can access the class. For example public class can be accessed by classes from any package, if no modifier is written the class can be accessed by classes from the same package only.

    Modifier of constructor, method and field has the same meaning. However private and protected have more sense. Private can be accessed from the current class only. Protected from its subclasses as far as from just classes from the same package.

    Concerning to your question about constructor. Class can have several constructors. Some of them can be private, some other public. You are right that there is no sense to make constructor public if class is package protected: no-one outside package can call this class anyway.

    This is exactly like writing public constructors for abstract classes. Since abstract class cannot be instantiated itself its constructors should be protected or private although compiler does not care about this.

    BTW using default package is not commonly used and not recommended technique.

    0 讨论(0)
提交回复
热议问题