Will the compiler-generated default constructor be public?

前端 未结 7 1368
小鲜肉
小鲜肉 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 06:08

    Depends on class visibility. For your class dafault constructor is going to be public.

    In a class type, if the class is declared public, then the default constructor is implicitly given the access modifier public (§6.6); if the class is declared protected, then the default constructor is implicitly given the access modifier protected (§6.6); if the class is declared private, then the default constructor is implicitly given the access modifier private (§6.6); otherwise, the default constructor has the default access implied by no access modifier.

    From here.

提交回复
热议问题