Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 in dealing with an inner class issue, I found maybe 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 there is a subclass of Outer in another package:

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, that error is solved. That's why I'm thinking that the modifier of the default constructor is not public! So could anyone tell me what the access modifier of the default constructor is?

like image 375
jason Avatar asked Feb 25 '14 07:02

jason


People also ask

What is default access modifier of constructor in Java?

The default access modifier is also called package-private, which means that all members are visible within the same package but aren't accessible from other packages: package com.

Which access specifier is used for constructor in Java?

Access specifiers/modifiers allowed with constructors Modifiers public, protected and, private are allowed with constructors. We can use a private constructor in a Java while creating a singleton class.

What is the default access specifier of Java?

The default visibility is known as “package-private” (though you can't use this explicitly), which means the field will be accessible from inside the same package to which the class belongs.

Is default an access modifier?

Default: When no access modifier is specified for a class, method, or data member – It is said to be having the default access modifier by default.


1 Answers

I thought its access modifier is public, but when I deal with a inner class issue, I found maybe I was wrong.

Yup. Indeed, I found myself in the same situation a couple of years ago. I was surprised by an error (through Guice injection, which made it slightly harder to find).

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

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.

So in this case, your constructor is implicitly protected.

like image 69
Jon Skeet Avatar answered Sep 22 '22 18:09

Jon Skeet