What's the access modifier of the default constructor in java?

前端 未结 3 1776
一整个雨季
一整个雨季 2020-12-23 14:15

We all know that if we don\'t specifically define a constructor, the compiler inserts an invisible zero-parameter constructor. I thought its access modifier was public, but

3条回答
  •  天涯浪人
    2020-12-23 14:56

    I would like to point out one more thing that I recently got. If you define a default constructor for your class then it's acess specifier will be what you assign. For example,

     public class A{
           A(){
           // do some stuff
           }
        }
    

    Here the access specifier of the default constructor is package access and not public access (that of the class). However

     public class A{
           // no constructor is defined
        }
    

    Here the compiler will sympathize with you and give you a default constructor whose access specifier will be same as the class , that is public.

提交回复
热议问题