"The constructor is not visible" error
Asked Answered
F

4

6

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 ?

Fluid answered 27/8, 2014 at 14:52 Comment(3)
Surely "second" is in the same package as "first". Remember: Default access (i.e No modifier) is visible to classes in the same package. And, the Other class is in other package different from first.Crelin
In what packages are First, Second and the class(es) where you try to instantiate them?Abeu
"Access modifiers" in Java are a bit confusing and not especially "regular". There's public, private, protected, and "no modifier". public can be accessed from anywhere, private only from within the same class, protected from a subclass, and "no modifier" from any class in the same "package".Fennel
C
15

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.

Cadwell answered 27/8, 2014 at 14:54 Comment(1)
Subclasses too can't call a superclass constructor with no modifierTeshatesla
W
7

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.

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
Whiteman answered 27/8, 2014 at 14:57 Comment(0)
S
2

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?

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

Problem with : Calling a method from a superclass

Stereotype answered 27/8, 2014 at 14:56 Comment(1)
Subclasses too can't call a superclass constructor with no modifierTeshatesla
S
0

just make your constructor public

Sulfathiazole answered 18/1, 2022 at 3:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.