Default access modifier for a Java constructor

前端 未结 4 626
悲哀的现实
悲哀的现实 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 06:55

    JLS 8.8.9 Default Constructor

    If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided:

    if the class is declared public, then the default constructor is implicitly given the access modifier public;
    if the class is declared protected, then the default constructor is implicitly given the access modifier protected;
    if the class is declared private, then the default constructor is implicitly given the access modifier private; otherwise,
    the default constructor has the default access implied by no access modifier.

提交回复
热议问题