[Solved] Default constructor issue when creating an array Java


The error message tells you exactly what’s wrong. There is no declared variable by the name of array.

public class ArrayObjects<E> implements SomeImp<E> {
    int maxCapacity, actualSize;
    E[] array; // <- The missing declaration

    @SuppressWarnings("unchecked") // <- Suppress the "unchecked cast" warning
    public ArrayObjects() {
        maxCapacity = 10;
        array = (E[]) new Object[maxCapacity];
    }
}

As far as the unchecked cast goes, the best thing you can do there is to suppress it as shown above.

3

solved Default constructor issue when creating an array Java