Levon Asatryan
Levon Asatryan

Reputation: 195

Java class compile time error "Cannot access interface"

I have an interface stores which has two methods getName() and getAddres(), and I have a class Market which implements Stores This is my code:

public interface Stores {
    public String getName();
    public String getAddress();
}

And the concrete class:

public class Market implements Stores {
    private String name;
    private String address;
    private int size;

    public Market(String name, String address, int size) {
        this.name = name;
        this.address = address;
        this.size = size;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getAddress() {
        return address;
    }

    public int getSize() {
        return size;
    }
}

I get errors in constructor regarding this.name=name, this.address=address and this.size=size saying that "Cannot access stores". Any ideas why?

Upvotes: 13

Views: 16030

Answers (6)

manuelvigarcia
manuelvigarcia

Reputation: 2094

In my case, I had the problem directly with maven, on the command line.

The solution was twofold: update the compiler plugin (from 2.2 to 3.10), so it would complain correctly about the class file version (instead of just telling "cannot access"); and compile the dependency (containing the class that "cannot access") with the same java version (11 instead of 17) as the project that was giving the error, so they would have the same java version.

Upvotes: 0

Mounir bkr
Mounir bkr

Reputation: 1635

  • just restart intellij.

or:

  • invalidat and rebuilt caches:

    file -> invalidate caches/restart -> invalidate and restart.

and it will solve your problem

Upvotes: 1

chemikos
chemikos

Reputation: 1

I had the same problem with IntelliJ. My solution is quite simple. Go to the interface file, delete the word interface and enter it again.

Upvotes: 0

Tushar
Tushar

Reputation: 1155

I have faced this issue in IntelliJ multiple times. It clearly doesn't happen due to any compilation error in the code. And nothing seems to help, apart from killing the IDE and starting it again.

And I mean killing the IDE completely, and not closing and opening the project. After doing this, the error vanishes.

(I feel weird writing this as an answer, but happened too many times with me, that I know how such errors make you question what you know about programming. Hope no one else has to waste their time on this)

Upvotes: 3

Skrrp
Skrrp

Reputation: 695

It's 2020 and I'm still having this issue in IntelliJ Idea. Sometimes a restart of the IDE is enough but today I had to File -> Invalidate Caches / Restart in order for it to recognise a new interface that I had written. Refused to recognise it in package local as well as when explicitly imported to other packages.

Upvotes: 17

Przemek
Przemek

Reputation: 126

I've faced the same issue in Intetlij. It was very weird for me what had helped:

I had to change any value in pom.xml (I've used "mainClass"). Then change it back (since value was correct). Project view was refreshed and class starts to see the interface.

Upvotes: 10

Related Questions