[java] Classes cannot be accessed from outside package

I have two packages. The class I want to import from the first package is declared as PUBLIC. Despite, when I test a file from the second package it shows me compilation errors like this:

PUBLICclass is not public in mypackage; cannot be accessed from outside package

I tried to add a public constructor to the class from the first package, but it doesn't make any difference.

Do you have any ideas? I use Netbeans 7.

The class from the first package looks like below:

public class PUBLICclass extends AbstractClass { public PUBLICclass() { } }

This question is related to java

The answer is


Maybe you should try removing "new" keyword and see if works. Because last time I got this error when I tried creating Typeface something like this:

Typeface typeface = new Typeface().create("Arial",Typeface.BOLD);

Check the default superclass's constructor. It need be public or protected.


closeDrawers(boolean) is not public in android.support.v4.widget.DrawerLayout. Cannot be accessed from outside package

@Override
public void onBackPressed() {
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
       super.onBackPressed();
    }
}

public SmartSaverCals(Context context)
{
    this.context= context;
}

add public to Your constructor.in my case problem solved


Note that the default when you make a class is not public as far as packages are considered. Make sure that you actually write public class [MyClass] { when defining your class. I've made this mistake more times than I care to admit.


Do you by any chance have two PUBLICclass classes in your project, where one is public (the one of which you posted the signature here), and another one which is package visible, and you import the wrong one in your code ?