What is the default constructor access modifier in java?

We all know that unless we specifically define a constructor, the compiler inserts an invisible constructor of the null parameter. I thought its access modifier was public, but when I ran into an inner class problem, I found that I was wrong. Here is my code:

public class Outer { protected class ProtectedInner { // adding a public constructor will solve the error in SubOuterInAnotherPackage class //public ProtectedInner() {} } } 

And in another package there is a subclass of Outer :

 public class SubOuterInAnotherPackage extends Outer { public static void main(String[] args) { SubOuterInAnotherPackage.ProtectedInner protectedInner = new SubOuterInAnotherPackage().new ProtectedInner(); // Error!! Can't access the default constructor } } 

You will get an error in the main() method, but if you add a public constructor to the ProtectedInner class, this error will be resolved. Therefore, I think that the default constructor modifier is not public! So can anyone tell me what the default constructor access modifier is?

+25
java access-specifier
Feb 25 '14 at 7:14
source share
3 answers

I thought its access modifier was public, but when I run into an inner class problem, I found that I might have made a mistake.

Yeah. Indeed, I found myself in the same situation a couple of years ago. I was surprised by the mistake (via Guice injection, which made it a little harder to find).

The key is to check the specification, in this case section 8.8.9 :

In a type class, if the class is declared public, then the default constructor implicitly receives a public access modifier (ยง6.6); if the class is declared protected, then the default constructor implicitly receives a security access code (ยง6.6); if the class is declared private, then the default constructor implicitly gets the available access modifier (ยง6.6); otherwise, the default constructor has default access implied by the access modifier.

So, in this case, your constructor is implicitly protected .

+37
Feb 25 '14 at 7:16
source share

In addition to what John said pretty well, here is an example image for visual guys.

If there is no constructor in the class, the compiler automatically creates a default constructor.

Here is an example that successfully displays the rule above:

enter image description here

For more information see here .

+1
Jan 18 '16 at 13:33
source share

I would like to mention one more thing that I recently received. If you define a default constructor for your class, then the acess qualifier is what you assigned. For example,

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

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

  public class A{ // no constructor is defined } 

Here, the compiler will sympathize with you and provide you with a default constructor, the access specifier of which will be the same as the public class.

+1
03 Sep '17 at 5:55 on
source share



All Articles