开发者

Getting an error about an undefined method(constructor) when I'm looking right at it? (Java)

I'm getting an error here that says I haven't defined a method, but it is right in the code.

class SubClass<E> extends ExampleClass<E>{
      Comparator<? super E> c;
      E x0;

      SubClass (Comparator<? super E> b, E y){
           this.c = b;
           this.x0 = y;
      }

      ExampleClass<E> addMethod(E x){
           if(c.compare(x, x0) < 0)
               return new OtherSubClassExtendsExampleClass(c, x0, SubClass(c, x)); 
               //this is where I get the error                        ^^^^^^
      }

I did define that Constructor for SubC开发者_如何转开发lass, why is it saying that I didn't when I try to pass it in that return statement?


You probably want new SubClass(c, x) instead of SubClass(c, x). In java you invoke constructor in different way than method: with new keyword.

More on the subject.


I think you want it to be:

                                       // new is needed to invoke a constructor 
return new OtherSubClassExtendsExampleClass(c, x0, new SubClass(c, x));


As rightly pointed out by others there is missing new required to invoke constructor.

Whats happening in your case here is that due to missing new your call is treated as method call, And in your class there is not method SubClass(c, x). And Error undefined method is correct in your case as there is no method named SubClass(c, x)

You need to correct same :

return new OtherSubClassExtendsExampleClass(c, x0, new SubClass(c, x));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜