• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

generics error

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i am getting this error after compiling :



so please suggest me where i am wrong.


Thanks,
karthik swamy.



 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and please use code tag when you post code!
 
karthik swamy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic