1

I have to build a stack of comparable objects from a given interface. Inside the class, this is my constructor:

public S()
{
   Comparable[] arr = new Comparable[INITSIZE];
   size = 0;
}

Now, in every method where the array appears, for example:

public void push(Comparable x)
{
   arr[size++] = x;
}

I get cannot find symbol error related to arr while compiling. Why?

  • Cannot find *which* symbol? What is the exact error message? – Andy Turner Feb 19 '16 at 12:12
  • 1
    `Comparable[] arr = null;` declare inside of class but outside of any method or constructor and Initialize `arr = new Comparable[INITSIZE];` it in constructor. Otherwise `arr` not visible to other methods since it is local variable in constructor. – SatyaTNV Feb 19 '16 at 12:12

1 Answers1

2

I get cannot find symbol error related to arr while compiling.

Declare arr inside of the class but outside of any method or constructor.

public class S{

    Comparable[] arr;  
}

and Initialize it in constructor.

public S()
{
    arr = new Comparable[INITSIZE];
}

Otherwise arr not visible to other methods and you will get cannot find symbol error related to arr while compiling since it is local variable in constructor.

public class S{

        Comparable[] arr = null; 

        public S()
        {
             arr = new Comparable[INITSIZE];
        }

    }
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31