Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find symbol when referring to an inner class in another file

Tags:

java

So I have this project I am working on and I need to create my own ArrayList class for it. I have all my methods defined, but when I compile I keep getting a "cannot find symbol" error for three of my methods. I have an interface that looks like this:

public interface FBList {

    public int size();

public void insert(int i, Person person);

public Person remove(int i);

public Person lookUp(int i);

/**
*A class that defines a person
**/
public class Person {
    private String id;
    private long phoneNum;

    public Person(String personID, long phoneNum){
        id = personID;
        phoneNum = phoneNum;
    }
}

As you can see, I have an inner class in there. I am trying to use that class in my other file which implements this interface. Currently, the three methods in my other file giving me the issue are as follows:

/**
    * A method to expand the size of the array if the array is too small
    * @param i  One minus the place in the list where the component will be inserted
    * @param Person The person to be put in the list
    **/
    protected void expandInsert(int i, Person person){
        Person[] temp = new Person[arrayList.length * 2];
        for(int index = 0; index < temp.length; index++){
            if( i != index){
                if(i > 0)
                    temp[index] = arrayList[index];
                if(i == 0)
                    temp[index + 1] = arrayList[index];
            }
            else{
                temp[i] = person;
                i = 0;
                index--;
            }
        }
        arrayList = temp;
    }

/**
    * Inserts a new component at the end of a list by creating a new list longer that then last
    * @param i The place in the list where the component will be inserted
    * @param Person The person to be added to the list
    **/

protected void insertAtEnd(int i, Person person){
    Person[] temp = new Person[arrayList.length + 5];
    for(int index = 0; index < temp.length; index++){
        if(index != i){
            temp[index] = arrayList[index];
        }
        else{
            temp[index] = person;
        }
    }
    arrayList = temp;
}

/**
* Shrinks the array by one by removing one component from the array
* @param i The index to be removed
**/

protected void shrink(int i){
    Person[] temp = new Person[arrayList.length - 1];
    for (int index = 0; index < arrayList.length ; index++ ) {
        if (index < i) {
            temp[index] = arrayList[index];
        }
        else if (index == i){
            removedPerson = arrayList[index];
            temp[index] = arrayList[index + 1];
        }
        else{
            temp[index - 1] = arrayList[index];
        }
    }
}

All of these files are in the same folder, so there shouldn't be an issue there. I am using terminal to compile by typing "javac FBArrayList.java". My compiler output looks like this:

 FBArrayList.java:106: cannot find symbol
symbol  : method expandInsert(int,FBList.Person)
location: class FBList.Person[]
            arrayList.expandInsert(i, person);
                     ^
FBArrayList.java:108: cannot find symbol
symbol  : method insertAtEnd(int,FBList.Person)
location: class FBList.Person[]
            arrayList.insertAtEnd(i, person);
                     ^
FBArrayList.java:118: cannot find symbol
symbol  : method shrink(int)
location: class FBList.Person[]
        arrayList.shrink(i);
                 ^
3 errors
like image 390
David Avatar asked Feb 17 '23 13:02

David


1 Answers

Since Person is an inner class, its name needs to be qualified with the name of the outer class:

protected void expandInsert(int i, FBList.Person person){
    FBList.Person[] temp = new FBList.Person[arrayList.length * 2];
    ...
    // and so on...
}

EDIT: Removed a suggestion to make class static because the class is nested inside an interface. The suggestion would have been required for classes nested inside classes; for interfaces, static is optional.

like image 173
Sergey Kalinichenko Avatar answered Apr 29 '23 12:04

Sergey Kalinichenko