user3262652
user3262652

Reputation: 19

Symbol not found error in wildcard

This is the source code for my program.. On line 22, in the function sameValue(Gen ob), there is an error which says that it cannot find symbol 'ob'. I am not getting as to, why?

class Gen1<T extends Number>
{
    T o;

    Gen1(T o)
    {
        this.o = o;
    }

    String getType()
    {
        return o.getClass().getName();
    }

    double getValue()
    {
        return o.doubleValue();
    }

    boolean sameValue(Gen1 <?> ob)
    {
      double x = ob.doubleValue();
        if (getValue() == x)
            return true;
        else
        return false;
    }
}

class Gen1Example{

    public static void main(String[] argv)
    {
        Gen1<Integer> o1 =new Gen1<Integer>(120);
        System.out.println(o1.getType());
        Gen1<Double> o2 =new Gen1<Double>(120.0);
        System.out.println(o2.getType());
        //Gen1<String> o2 =new Gen1<String>("This is a test");
        //System.out.println(o2.getType());
        System.out.println(o1.getValue());
        System.out.println(o2.getValue());
        System.out.println(o1.sameValue(o2));
    }
}

Upvotes: 1

Views: 48

Answers (2)

Vampire
Vampire

Reputation: 38639

You misread the error message. It says

Gen1Example.java:22: error: cannot find symbol
      double x = ob.doubleValue();
                   ^
  symbol:   method doubleValue()
  location: variable ob of type Gen1<?>
1 error

It complains that in ob of type Gen1<?> there is no method doubleValue. You probably meant ob.getValue().

Upvotes: 1

Ti Strga
Ti Strga

Reputation: 1390

Just because T extends the Number class doesn't mean that Gen1 does. You probably wanted something like ob.getValue() (or ob.o.doubleValue() but this exposes details of implementation) instead.

Upvotes: 0

Related Questions