wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes generics error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "generics error" Watch "generics error" New topic
Author

generics error

karthik swamy
Ranch Hand

Joined: Mar 14, 2011
Posts: 42


i am getting this error after compiling :



so please suggest me where i am wrong.


Thanks,
karthik swamy.



Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

your *public T getData() * expect the generic return type T . but you have returned a string!

so change the method return type as String[preferable approach] else cast the string to T and return as in *return (T)s*;

<edit>added clarity</edit>
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and please use code tag when you post code!
karthik swamy
Ranch Hand

Joined: Mar 14, 2011
Posts: 42
Seetharaman Venkatasamy wrote:your *public T getData() * expect the generic return type T . but you have returned a string!

so change the method return type as String[preferable approach] else cast the string to T and return as in *return (T)s*;

<edit>added clarity</edit>


thanks its working,but can it wil work for integer type
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

karthik swamy wrote:but can it wil work for integer type

actually *return (T)s* this lead you in confusion. that is why I said first approach is preferable. Yes , it will work for an Integer type also
karthik swamy
Ranch Hand

Joined: Mar 14, 2011
Posts: 42
Seetharaman Venkatasamy wrote:
karthik swamy wrote:but can it wil work for integer type

actually *return (T)s* this lead you in confusion. that is why I said first approach is preferable. Yes , it will work for an Integer type also



but generics is used for auto typecast then why we are typecasting like return (t)s
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

karthik swamy wrote:but generics is used for auto typecast then why we are typecasting like return (t)s

your T getData implementation force us to do that! you should reimplement[even signature of method] the method!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: generics error