Default access modifier for a Java constructor

前端 未结 4 606
悲哀的现实
悲哀的现实 2020-12-01 06:25

Can anybody explain what the default access modifier is for an explicit no-arg constructor (and other constructors)?

4条回答
  •  遥遥无期
    2020-12-01 07:07

    It differs depending on whether you're writing a constructor for a regular class or an enum:

    • For classes, the answer is given by JLS §6.6.1:

      A class member or constructor declared without an access modifier implicitly has package access.

    • For enums, the answer is given by JLS §8.9.2:

      In an enum declaration, a constructor declaration with no access modifiers is private.

      (Enum constructors are always private to prevent other classes instantiating more enum constants.)

提交回复
热议问题