java中默认构造函数的访问修饰符是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22007143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the access modifier of the default constructor in java?
提问by jason
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:
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, 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?
您将在该main()
方法中得到一个错误,但是如果您向ProtectedInner
该类添加一个公共构造函数,该错误就解决了。这就是为什么我认为默认构造函数的修饰符不是公开的!那么谁能告诉我默认构造函数的访问修饰符是什么?
采纳答案by Jon Skeet
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).
是的。事实上,几年前我发现自己处于同样的境地。我对一个错误感到惊讶(通过 Guice 注入,这使得它更难找到)。
The key is to check the spec, in this case section 8.8.9:
关键是检查规范,在这种情况下是第 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.
在类类型中,如果类被声明为 public,则默认构造函数被隐式赋予访问修饰符 public(第 6.6 节);如果类被声明为受保护,则默认构造函数被隐式赋予访问修饰符 protected(第 6.6 节);如果类被声明为私有,则默认构造函数被隐式赋予访问修饰符私有(第 6.6 节);否则,默认构造函数具有无访问修饰符隐含的默认访问权限。
So in this case, your constructor is implicitly protected
.
所以在这种情况下,你的构造函数是隐式的protected
。
回答by thodorisbais
In addition to what Jon pretty well stated, here is an image example, for the visual guys.
除了乔恩说得很好之外,这里还有一个图像示例,供视觉人员使用。
If there is no constructor in a class, compiler automatically creates a default constructor.
如果类中没有构造函数,编译器会自动创建一个默认构造函数。
Here is an example that successfully depicts the above rule:
下面是一个成功描述上述规则的例子:
For further reference, please refer here.
如需进一步参考,请参阅此处。
回答by Surendra Sharma
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.
这里编译器会同情你,并为你提供一个默认构造函数,它的访问说明符将与 class 相同,即 public。