Will the compiler-generated default constructor be public?

前端 未结 7 1289
小鲜肉
小鲜肉 2021-01-12 05:36

When I write a class Widget.java

public class Widget {
    int data;
    String name;
}

will the compiler-generated constructo

相关标签:
7条回答
  • 2021-01-12 05:47

    It will be public Widget() {}

    0 讨论(0)
  • 2021-01-12 05:50

    As classes visibility is public, it will always be a public constructor.

    0 讨论(0)
  • 2021-01-12 05:51

    As said in JLS

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

    1. If the class is declared public, then the default constructor is implicitly given the access modifier public;
    2. If the class is declared protected, then the default constructor is implicitly given the access modifier protected;
    3. If the class is declared private, then the default constructor is implicitly given the access modifier private;
    4. Otherwise, the default constructor has the default access implied by no access modifier
    0 讨论(0)
  • 2021-01-12 05:52

    It depends on your class visibility.The compiler uses the class visibility and generates a no-arg default constructor with the same visibility

    0 讨论(0)
  • 2021-01-12 05:55

    It will be public as the class visibility is public

    public Widget() {}
    
    0 讨论(0)
  • 2021-01-12 05:55

    If your class is public then the default constructor would be public so in your case, Since the Widget class is public its default constructor supplied by the compiler would also be public. See this

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