deem
deem

Reputation: 1894

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() { } }

Upvotes: 59

Views: 167866

Answers (8)

Christian Vincenzo Traina
Christian Vincenzo Traina

Reputation: 10464

If you are using the @Data decorator from lombok, you may receive this error since it automatically creates a not public constructor.

So if you have:

@Data
public class Foo {
  private String bar;
}

new Foo("hello"); // Not public constructor
new Foo(); // Not public constructor

Then you need to decorate your class with @AllArgsConstructor or @NoArgsConstructor

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Foo {
  private String bar;
}

new Foo("hello"); // Now it works
new Foo(); // Also this works

Upvotes: 1

mprabhat
mprabhat

Reputation: 20323

Let me guess

Your initial declaration of class PUBLICClass was not public, then you made it Public, can you try to clean and rebuild your project ?

Upvotes: 68

SMBiggs
SMBiggs

Reputation: 11726

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.

Upvotes: 0

Subroto Basak Shawon
Subroto Basak Shawon

Reputation: 51

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();
    }
}

Upvotes: 0

MbPCM
MbPCM

Reputation: 497

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);

Upvotes: 0

saigopi.me
saigopi.me

Reputation: 14938

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

add public to Your constructor.in my case problem solved

Upvotes: 24

deldev
deldev

Reputation: 1386

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

Upvotes: 7

Robin
Robin

Reputation: 36621

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 ?

Upvotes: 0

Related Questions