In Java, what happens when you have a method with an unspecified visibility keyword?

前端 未结 3 1217
萌比男神i
萌比男神i 2021-01-02 04:20

I have been working with android for a few years now, not once have I had a teacher or anyone to tell me what to do. This whole time I have wondered to myself this.

3条回答
  •  隐瞒了意图╮
    2021-01-02 04:52

    Java has four levels of visibility: public, protected, (default), private. The meaning of these is as follows:

    1. public - makes your methods accessible to any other class.
    2. protected - makes your methods accessible to any class in the same package OR any subclass of your class.
    3. (default, i.e. no modifier) - makes your methods accessible only to classes in the same package.
    4. private - makes your methods accessible only to the current class.

    The same rules apply when specifying the access modifiers on classes, methods and fields.

提交回复
热议问题