Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

java - "The constructor is not visible" error

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 First class, Eclipse generates me an error ("The constructor is not visible"), I have to add public to the constructor of First :

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

But when I instantiate my Second class, I have no error :

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

Notes: I instantiate First in an other class, but I instantiate Second in First. First and Second are in the same package.

Can you explain me why ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...