"Illegal generic instance type" when using instanceof for an internal class type?

I encoded something like this in NetBeans:

public class Grafo<V, E>
{
    class Par
    {
        int a, b;
        Par(int a, int b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public boolean equals(Object ob)
        {
            if(ob instanceof Par) {
                Par p = (Par)ob;
                return this.a==p.a && this.b==p.b;
            }

            return false;
        }
    }

    //stuff...
} //end of class Grafo

Error in equals () method from inner class "Par".

NetBeans says the error is "an illegal common type of instanceof." The error is in the line below.

            if(ob instanceof Par) {

What is the cause of the error?

+3
source share
2 answers

Try ob instanceof Grafo<?,?>.Par

I think the compiler thinks it ob instanceof Parincludes runtime checking for type type parameters; those. that it is equivalent ob instanceof Grafo<V,E>.Par. But tests instanceofcannot check type parameters.

+6
source
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object ob)
{
    if(ob instanceof Grafo.Par) {
        Par p = (Par)ob;
        return this.a==p.a && this.b==p.b;
    }

    return false;
}

static

+3

Source: https://habr.com/ru/post/1725514/


All Articles