Java “构造函数不可见”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25530382/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:35:15  来源:igfitidea点击:

"The constructor is not visible" error

javaconstructorpublic

提问by BnJ

I have two classes :

我有两个班级:

First, with one constructor :

首先,使用一个构造函数:

public class First {

    First (ObjectA myObjectA) {
        //do stuff
    }
}

And Second, with two constructors :

其次,有两个构造函数:

public class Second {

    Second (ObjectB myObjectB) {
        //do something...
    }

    Second (ObjectC myObjectC) {
        //do something else...
    }
}

When I want to instantiate my Firstclass, Eclipse generates me an error ("The constructor is not visible"), I have to add publicto the constructor of First:

当我想实例化我的First类时,Eclipse 会生成一个错误(“构造函数不可见”),我必须添加public到以下构造函数中First

First first = new First(myObject); //Error : "The constructor is not visble"

But when I instantiate my Secondclass, I have no error :

但是当我实例化我的Second类时,我没有错误:

Second second = new Second(myObjectC); //No error...

Notes: I instantiate Firstin an other class, but I instantiate Secondin First. Firstand Secondare in the same package.

注意:我First在另一个类中实例化,但我SecondFirst. First并且Second在同一个包中。

Can you explain me why ?

你能解释一下为什么吗?

采纳答案by J4v4

No access modifier for your constructor makes it package private. Assuming that First and Second are in the same package, you can call Second's constructors from first. Another class from another package, however, cannot access any of the constructors.

您的构造函数没有访问修饰符使其成为包 private。假设 First 和 Second 在同一个包中,您可以从 first 调用 Second 的构造函数。但是,另一个包中的另一个类无法访问任何构造函数。

回答by Suresh Atta

Since you are not mentined any modifier the access modifier is now default, that means it is visible only within its own package

由于您没有提到任何修饰符,因此访问修饰符现在是默认值,这意味着它仅在其自己的包中可见

If you try to use it out side the package, you'll face the current error.

如果您尝试在包外使用它,您将面临当前的错误。

Try to read :What is the default access modifier in Java?

尝试阅读:Java 中的默认访问修饰符是什么?

If you did'nt get understood what @BackSlash is commenting, check the below link

如果您不明白@BackSlash 在评论什么,请查看以下链接

Problem with : Calling a method from a superclass

问题:从超类调用方法

回答by Thusitha Thilina Dayaratne

Your class must be in 2 packages. If you don't mention any explicit access modifier Java will consider them as default access modifier. Then you can only access them through the same package only.

您的课程必须在 2 个包中。如果您没有提及任何显式访问修饰符,Java 会将它们视为默认访问修饰符。那么你只能通过同一个包访问它们

Access Modifiers (From least access to highest access)

访问修饰符(从最少访问到最高访问)

  1. private- Only within same class
  2. default- Only within same package
  3. protected- same package + children classes in other packages
  4. public- From anywhere
  1. 私人- 仅在同一班级内
  2. 默认- 仅在同一个包内
  3. 受保护- 相同的包 + 其他包中的子类
  4. 公共- 从任何地方