• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic error  RSS feed

 
Sushant Kunwara
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

In the following code I am getting error .



The error is as follows :


G:\Java>javac Main.java
Main.java:15: error: incompatible types: String cannot be converted to T
return new String("abc");
^
where T is a type-variable:
T extends Object declared in method <T>foo(T)
1 error


Why am I getting this error ? Wont T = String ?
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have called foo() generically (I think that's the term). You need to pass the type in angle brackets.

[Edit] Sorry, wrong answer. You need to return a type T in foo().
 
Sushant Kunwara
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You have called foo() generically (I think that's the term). You need to pass the type in angle brackets.

[Edit] Sorry, wrong answer. You need to return a type T in foo().


I have done that . ... <T> " T " foo(...)
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You declare that foo() returns T but you are actually returning a String. What if someone passed Integer to foo()? It can't return an Integer if it's returning a String.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!