Java class compile time error "Cannot access interface"

10,634

Solution 1

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.

Solution 2

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.

Solution 3

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)

Share:
10,634
Levon Asatryan
Author by

Levon Asatryan

Updated on June 05, 2022

Comments

  • Levon Asatryan
    Levon Asatryan almost 2 years

    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?

  • Tugrul ASLAN
    Tugrul ASLAN over 5 years
    Similar situation in IntelliJ I deleted unused maven auto created test classes and Rebuilt the project, worked like a charm
  • Miguel Tomás
    Miguel Tomás about 3 years
    this worked for me on android studio 4.2 C15 SDK 30
  • Sanjay Verma
    Sanjay Verma almost 3 years
    Hey there I'm from 2021, I still have the same issue :/